-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Changes from 2 commits
8b03d6a
e6eb0d3
e6bd7eb
4067e25
a23b16b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* 두 선을 선택해 물을 담을 수 있는 최대 면적을 구하는 문제 | ||
* | ||
* 가로 길이: 두 선 사이의 거리(인덱스 차이) | ||
* 세로 길이: 두 선 중 더 짧은 높이(물은 낮은 쪽으로 넘치기 때문) | ||
* 면적 = 가로 길이 × 세로 길이 | ||
* | ||
* 양쪽 끝에서 시작하는 두 포인터를 사용 | ||
* 투 포인터 방식이 효율적인 이유: 항상 더 작은 높이를 가진 쪽을 이동시키면 최대 면적을 놓치지 않기 때문 | ||
* 더 큰 높이 쪽을 이동시키면 가로 길이는 줄어들고, 세로 길이는 같거나 더 작아져서 면적이 줄어들 수밖에 없음 | ||
* | ||
* 시간 복잡도: 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; | ||
}; |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(']'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
문제 풀이 로직, 복잡도 분석, 그리고 코드 라인별 코멘트를 잘 정리하신 걸 보고 많이 배우고 갑니다...! 🤩