Skip to content

Commit fe8a960

Browse files
authored
Merge pull request #894 from KwonNayeon/main
[KwonNayeon] Week 6
2 parents 7e7fc1f + 83f538a commit fe8a960

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Constraints:
3+
1. n == height.length
4+
2. 2 <= n <= 10^5
5+
3. 0 <= height[i] <= 10^4
6+
7+
Time Complexity:
8+
-
9+
10+
Space Complexity:
11+
-
12+
"""

โ€Žvalid-parentheses/KwonNayeon.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Constraints:
3+
1. 1 <= s.length <= 10^4
4+
2. s consists of parentheses only '()[]{}'
5+
6+
Time Complexity: O(n)
7+
- ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž๋ฅผ ํ•œ ๋ฒˆ์”ฉ๋งŒ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(n)
8+
- ๊ฐ ๋ฌธ์ž์— ๋Œ€ํ•œ ์—ฐ์‚ฐ(push, pop)์€ O(1)
9+
10+
Space Complexity: O(n)
11+
- ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ๋ฌธ์ž๊ฐ€ ์—ฌ๋Š” ๊ด„ํ˜ธ์ผ ๋•Œ ์Šคํƒ์— ๋ชจ๋‘ ์ €์žฅ
12+
- ๋”ฐ๋ผ์„œ ์ž…๋ ฅ ํฌ๊ธฐ์— ๋น„๋ก€ํ•˜๋Š” O(n) ๊ณต๊ฐ„ ํ•„์š”
13+
14+
ํ’€์ด๋ฐฉ๋ฒ•:
15+
1. ์Šคํƒ์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Š” ๊ด„ํ˜ธ('(', '{', '[')๋ฅผ ์ €์žฅ
16+
2. Dictionary๋ฅผ ์‚ฌ์šฉํ•ด ๋‹ซ๋Š” ๊ด„ํ˜ธ์™€ ์—ฌ๋Š” ๊ด„ํ˜ธ์˜ ์Œ์„ O(1)๋กœ ๋งค์นญ
17+
3. ๋ฌธ์ž์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ:
18+
- ์—ฌ๋Š” ๊ด„ํ˜ธ๋Š” ์Šคํƒ์— ์ถ”๊ฐ€
19+
- ๋‹ซ๋Š” ๊ด„ํ˜ธ๊ฐ€ ๋‚˜์˜ค๋ฉด:
20+
a) ์Šคํƒ์ด ๋น„์–ด์žˆ๊ฑฐ๋‚˜
21+
b) ์Šคํƒ ์ตœ์ƒ๋‹จ์˜ ๊ด„ํ˜ธ๊ฐ€ ํ˜„์žฌ ๋‹ซ๋Š” ๊ด„ํ˜ธ์™€ ๋งค์นญ๋˜์ง€ ์•Š์œผ๋ฉด
22+
-> ์ž˜๋ชป๋œ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด
23+
- ๋งค์นญ๋˜๋Š” ๊ฒฝ์šฐ ์Šคํƒ์—์„œ pop
24+
4. ๋ชจ๋“  ์ˆœํšŒ๊ฐ€ ๋๋‚œ ํ›„ ์Šคํƒ์ด ๋น„์–ด์žˆ์–ด์•ผ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด
25+
"""
26+
class Solution:
27+
def isValid(self, s: str) -> bool:
28+
stack = []
29+
pairs = {')': '(', '}': '{', ']': '['}
30+
31+
for char in s:
32+
if char in '({[':
33+
stack.append(char)
34+
else:
35+
if not stack or stack[-1] != pairs[char]:
36+
return False
37+
stack.pop()
38+
39+
return len(stack) == 0

0 commit comments

Comments
ย (0)