File tree Expand file tree Collapse file tree 4 files changed +147
-0
lines changed
container-with-most-water
design-add-and-search-words-data-structure
longest-increasing-subsequence Expand file tree Collapse file tree 4 files changed +147
-0
lines changed Original file line number Diff line number Diff line change
1
+ '''
2
+ ์๊ฐ ๋ณต์ก๋: O(n)
3
+ - ๋ ํฌ์ธํฐ๋ฅผ ์ด๋ํ๋ฉด์ ๋ฐฐ์ด์ ํ ๋ฒ๋ง ์ํํ๋ฏ๋ก ์๊ฐ ๋ณต์ก๋๋ O(n)์
๋๋ค.
4
+
5
+ ๊ณต๊ฐ ๋ณต์ก๋: O(1)
6
+ - ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๋ณ์๋ง ์ฌ์ฉํ๋ฏ๋ก O(1)์
๋๋ค.
7
+ '''
8
+
9
+ from typing import List
10
+
11
+
12
+ class Solution :
13
+ def maxArea (self , height : List [int ]) -> int :
14
+ max_area = 0
15
+ left , right = 0 , len (height ) - 1
16
+
17
+ while left < right :
18
+ current_area = (right - left ) * min (height [left ], height [right ])
19
+ max_area = max (current_area , max_area )
20
+
21
+ if height [left ] < height [right ]:
22
+ left += 1
23
+ else :
24
+ right -= 1
25
+
26
+ return max_area
Original file line number Diff line number Diff line change
1
+ '''
2
+ * L: ๋จ์ด์ ๊ธธ์ด
3
+ ์๊ฐ๋ณต์ก๋: O(1)
4
+ - addWord(word): O(L), ์ต๋ ๋จ์ด ๊ธธ์ด๊ฐ 25๋ก ์ ํ๋๋ฏ๋ก ์ด ์์
์ ์์ ์๊ฐ์ ๊ฐ๊น์ต๋๋ค.
5
+ - search(word): O(L * 26^d), ์ฌ๊ธฐ์ 26์ ์ํ๋ฒณ ์๋ฌธ์์ ๊ฐ์๋ฅผ ์๋ฏธํฉ๋๋ค. d๋ ๋จ์ด ๋ด '.'์ ๊ฐ์์ธ๋ฐ, 2๋ก ์ ํ๋๋ฏ๋ก ์์ ์๊ฐ์ ๊ฐ๊น์ต๋๋ค.
6
+ ๊ณต๊ฐ๋ณต์ก๋:
7
+ - Trie ๊ตฌ์กฐ์ ์ ์ฅ๋๋ ๋ฌธ์์ ์์ ๋น๋กํฉ๋๋ค. ๋จ์ด์ ์ด ๊ธธ์ด๋ฅผ T๋ผ๊ณ ํ๋ฉด ๊ณต๊ฐ๋ณต์ก๋๋ O(T) ์
๋๋ค.
8
+ '''
9
+
10
+
11
+ class Trie :
12
+ def __init__ (self ):
13
+ self .children = {}
14
+ self .is_end_of_word = False
15
+
16
+ class WordDictionary :
17
+
18
+ def __init__ (self ):
19
+ # Trie์ ๋ฃจํธ ๋
ธ๋ ์ด๊ธฐํ
20
+ self .root = Trie ()
21
+
22
+ def addWord (self , word : str ) -> None :
23
+ current = self .root
24
+
25
+ for char in word :
26
+ if char not in current .children :
27
+ current .children [char ] = Trie ()
28
+
29
+ current = current .children [char ]
30
+
31
+ current .is_end_of_word = True
32
+
33
+ def search (self , word : str ) -> bool :
34
+ def dfs (node , i ):
35
+ if i == len (word ):
36
+ return node .is_end_of_word
37
+
38
+ char = word [i ]
39
+ if char == '.' :
40
+ # '.'์ธ ๊ฒฝ์ฐ ๋ชจ๋ ์์ ๋
ธ๋์ ๋ํด ํ์
41
+ for child in node .children .values ():
42
+ if dfs (child , i + 1 ):
43
+ return True
44
+ return False
45
+
46
+ if char not in node .children :
47
+ return False
48
+
49
+ return dfs (node .children [char ], i + 1 )
50
+
51
+ return dfs (self .root , 0 )
52
+
53
+
54
+
55
+ # Your WordDictionary object will be instantiated and called as such:
56
+ # obj = WordDictionary()
57
+ # obj.addWord(word)
58
+ # param_2 = obj.search(word)
Original file line number Diff line number Diff line change
1
+ '''
2
+ Dynamic programming ํ์ฉ
3
+
4
+ ์๊ฐ๋ณต์ก๋: O(n^2) - ๋ ๊ฐ์ ์ค์ฒฉ๋ ๋ฐ๋ณต๋ฌธ์ด nums ๋ฐฐ์ด์ ํ์ํจ
5
+ ๊ณต๊ฐ๋ณต์ก๋: O(n) - dp ๋ฐฐ์ด์ ์ซ์ ๊ฐ์(n)๋งํผ ๊ณต๊ฐ์ด ํ์ํจ
6
+ '''
7
+
8
+ def lengthOfLIS_n2 (nums ):
9
+ n = len (nums )
10
+ dp = [1 ] * n
11
+
12
+ for i in range (1 , n ):
13
+ for j in range (i ):
14
+ if nums [j ] < nums [i ]:
15
+ dp [i ] = max (dp [i ], dp [j ] + 1 ) # ์ด์ LIS ๊ธธ์ด์ 1 ์ถ๊ฐ
16
+
17
+ return max (dp ) # dp ๋ฐฐ์ด์ ์ต๋๊ฐ์ด ์ต์ฅ ๊ธธ์ด
18
+
19
+
20
+ '''
21
+ ์ด์งํ์ ํ์ฉ
22
+
23
+ ์๊ฐ๋ณต์ก๋: O(n log n) - ๊ฐ ์ซ์์ ๋ํด ์ด์ง ํ์(bisect_left)์ ์ํํจ
24
+ ๊ณต๊ฐ๋ณต์ก๋: O(n) - sub ๋ฆฌ์คํธ์ ์ต๋ n๊ฐ์ ์ซ์๊ฐ ์ ์ฅ๋ ์ ์์
25
+ '''
26
+
27
+ from bisect import bisect_left
28
+
29
+ def lengthOfLIS_nlogn (nums ):
30
+ sub = [] # ํ์ฌ๊น์ง ์ฐพ์ LIS์ ์ซ์๋ค์ ์ ์ฅ
31
+
32
+ for num in nums :
33
+ pos = bisect_left (sub , num ) # ์ฝ์
์์น๋ฅผ ์ด์ง ํ์์ผ๋ก ์ฐพ์
34
+
35
+ if pos == len (sub ):
36
+ sub .append (num ) # ์ฝ์
์์น๊ฐ sub์ ๋ฒ์ ๋ฐ์ด๋ฉด ์ซ์ ์ถ๊ฐ
37
+ else :
38
+ sub [pos ] = num # ์ฝ์
์์น๊ฐ ๋ฒ์ ์์ด๋ฉด ํด๋น ์์น์ ์ซ์๋ฅผ ํ์ฌ ์ซ์๋ก ๊ต์ฒด
39
+
40
+ return len (sub )
Original file line number Diff line number Diff line change
1
+ '''
2
+ ์๊ฐ ๋ณต์ก๋: O(n)
3
+ - ๋ฌธ์์ด s์ ๊ธธ์ด๋ฅผ n์ด๋ผ๊ณ ํ ๋, ๋ฌธ์์ด์ ๊ฐ ๋ฌธ์๋ฅผ ํ ๋ฒ์ฉ ์ํํ๋ฉฐ ์ฒ๋ฆฌํ๋ฏ๋ก O(n)์
๋๋ค.
4
+
5
+ ๊ณต๊ฐ ๋ณต์ก๋: O(n)
6
+ - ์คํ์ ์ด๋ฆฐ ๊ดํธ๋ฅผ ์ ์ฅํ๋ ๋ฐ ์ฌ์ฉ๋๋ ๊ณต๊ฐ์ด ์ต์
์ ๊ฒฝ์ฐ ๋ฌธ์์ด s์ ๊ธธ์ด n๊ณผ ๊ฐ์ ์ ์์ผ๋ฏ๋ก O(n)์
๋๋ค.
7
+ '''
8
+
9
+ class Solution :
10
+ def isValid (self , s : str ) -> bool :
11
+ stack = []
12
+ bracket_map = {")" : "(" , "}" : "{" , "]" : "[" }
13
+
14
+ for bracket in s :
15
+ if bracket in bracket_map :
16
+ if stack and stack [- 1 ] == bracket_map [bracket ]:
17
+ stack .pop ()
18
+ else :
19
+ return False
20
+ else :
21
+ stack .append (bracket )
22
+
23
+ return len (stack ) == 0
You canโt perform that action at this time.
0 commit comments