File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed
container-with-most-water Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change 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+ """
Original file line number Diff line number Diff line change 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
You canโt perform that action at this time.
0 commit comments