Skip to content
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

[KwonNayeon] Week 6 #894

Merged
merged 3 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions container-with-most-water/KwonNayeon.py
Copy link
Contributor

Choose a reason for hiding this comment

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

혹시 문제 풀이가 올라오지 않은 걸까요 :)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@TonyKim9401 리더님 안녕하세요! 이번 주는 너무 신경을 못 썼네요... 다음 주부터는 분발하겠습니다 댓글 감사합니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
Constraints:
1. n == height.length
2. 2 <= n <= 10^5
3. 0 <= height[i] <= 10^4

Time Complexity:
-

Space Complexity:
-
"""
39 changes: 39 additions & 0 deletions valid-parentheses/KwonNayeon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
Constraints:
1. 1 <= s.length <= 10^4
2. s consists of parentheses only '()[]{}'

Time Complexity: O(n)
- 문자열의 각 문자를 한 번씩만 순회하므로 O(n)
- 각 문자에 대한 연산(push, pop)은 O(1)

Space Complexity: O(n)
- 최악의 경우 모든 문자가 여는 괄호일 때 스택에 모두 저장
- 따라서 입력 크기에 비례하는 O(n) 공간 필요

풀이방법:
1. 스택을 사용하여 여는 괄호('(', '{', '[')를 저장
2. Dictionary를 사용해 닫는 괄호와 여는 괄호의 쌍을 O(1)로 매칭
3. 문자열을 순회하면서:
- 여는 괄호는 스택에 추가
- 닫는 괄호가 나오면:
a) 스택이 비어있거나
b) 스택 최상단의 괄호가 현재 닫는 괄호와 매칭되지 않으면
-> 잘못된 괄호 문자열
- 매칭되는 경우 스택에서 pop
4. 모든 순회가 끝난 후 스택이 비어있어야 올바른 괄호 문자열
"""
class Solution:
def isValid(self, s: str) -> bool:
stack = []
pairs = {')': '(', '}': '{', ']': '['}

for char in s:
if char in '({[':
stack.append(char)
else:
if not stack or stack[-1] != pairs[char]:
return False
stack.pop()

return len(stack) == 0
Loading