Skip to content

[clara-shin] WEEK 06 solutions #1420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions container-with-most-water/clara-shin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* 두 선을 선택해 물을 담을 수 있는 최대 면적을 구하는 문제
*
* 가로 길이: 두 선 사이의 거리(인덱스 차이)
* 세로 길이: 두 선 중 더 짧은 높이(물은 낮은 쪽으로 넘치기 때문)
* 면적 = 가로 길이 × 세로 길이
*
* 양쪽 끝에서 시작하는 두 포인터를 사용
* 투 포인터 방식이 효율적인 이유: 항상 더 작은 높이를 가진 쪽을 이동시키면 최대 면적을 놓치지 않기 때문
* 더 큰 높이 쪽을 이동시키면 가로 길이는 줄어들고, 세로 길이는 같거나 더 작아져서 면적이 줄어들 수밖에 없음
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제 풀이 로직, 복잡도 분석, 그리고 코드 라인별 코멘트를 잘 정리하신 걸 보고 많이 배우고 갑니다...! 🤩

*
* 시간 복잡도: O(n) - 배열을 한 번만 순회
* 공간 복잡도: O(1) - 추가 공간 사용 없음
*/
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function (height) {
let left = 0;
let right = height.length - 1;
let maxWater = 0;

while (left < right) {
// 현재 두 선으로 만들 수 있는 물의 양 계산
const width = right - left;
const minHeight = Math.min(height[left], height[right]);
const water = width * minHeight;

// 최대값 업데이트
maxWater = Math.max(maxWater, water);

// 더 작은 높이를 가진 쪽의 포인터를 이동시킴
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}

return maxWater;
};
37 changes: 37 additions & 0 deletions valid-parentheses/clara-shin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 괄호 문자열 유효성 검사
*
* 스택 자료구조 활용
* 1. 괄호 쌍을 매핑하는 객체를 생성하고 조건을 확인
* 2. 열린 괄호를 만나면 해당하는 닫힌 괄호를 스택에 직접 push
* 3. 닫는 괄호를 만났을 때, 스택이 비어있거나 짝이 맞지 않으면 false
* 4. 문자열을 모두 처리한 후, 스택이 비어있어야(문자열 길이가 0이어야) 모든 괄호가 올바르게 짝지어진 것(true)
*/

/**
* @param {string} s
* @return {boolean}
*/
var isValid = function (s) {
// 빈 문자열이나 홀수 길이는 유효하지 않음
if (s.length === 0 || s.length % 2 !== 0) return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 빠르게 종료할 수 있는 조건을 추가해두면 런타임 측면에서 확실히 좋아질 것 같네요!


const stack = [];

for (let i = 0; i < s.length; i++) {
const char = s[i];

if (char === '(') {
stack.push(')');
} else if (char === '{') {
stack.push('}');
} else if (char === '[') {
stack.push(']');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if문부터 두 번째 else if문까지 여는 괄호를 판단하는 동일한 조건과 stack.push()라는 동일한 동작을 구현하고 있기 때문에, 다음과 같이 해시맵을 사용한다면 반복되는 분기문 코드를 조금 더 줄일 수도 있을 것 같습니다~! (다만, 제가 자바스크립트를 잘 몰라서 문법에 오류가 있을 수 있습니다.. 😭)

const mapping = {
  '(': ')',
  '{': '}',
  '[': ']'
};

...

for (let char of s) {
  if (char in mapping) {
    stack.push(mapping[char]);
  } else {
    ...
    }
  }
}

} else if (stack.length === 0 || stack.pop() !== char) {
// 닫는 괄호를 만났을 때, 스택이 비어있거나 짝이 맞지 않음
return false;
}
}

return stack.length === 0;
};