Skip to content

Commit

Permalink
Merge pull request #894 from KwonNayeon/main
Browse files Browse the repository at this point in the history
[KwonNayeon] Week 6
  • Loading branch information
KwonNayeon authored Jan 18, 2025
2 parents 7e7fc1f + 83f538a commit fe8a960
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
12 changes: 12 additions & 0 deletions container-with-most-water/KwonNayeon.py
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

0 comments on commit fe8a960

Please sign in to comment.