File tree Expand file tree Collapse file tree
container-with-most-water
design-add-and-search-words-data-structure
encode-and-decode-strings
longest-increasing-subsequence
longest-substring-without-repeating-characters Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ class Solution :
2+ def maxArea (self , height : List [int ]) -> int :
3+ '''
4+ 1.๋ฌธ์ : ๊ฐ์ฅ ๋ง์ ์์ ๋ฌผ์ ์ ์ฅํ ์ ์๋ max value return
5+ 2.์กฐ๊ฑด
6+ - n: ๋์ด๋ฅผ ์๋ฏธ, ์ต์ = 5, ์ต๋ = 10^5
7+ - ์์๊ฐ ์ต์ = 0, ์ต๋ = 10^4
8+ 3.ํ์ด
9+ - ๋์ด๋ height[i], height[j] ์ค์ ์์ ๊ฐ, ๊ฐ๋ก๋ abs(i-j)
10+ - output = ๋์ด x ๊ฐ๋ก
11+ -> 2์ค loop ๋ O(n^2) ๋ก TLE ๋ฐ์.
12+ -> two pointer ๋ก O(n) ์ผ๋ก ํด๊ฒฐ!
13+ '''
14+
15+ n = len (height )
16+ maxArea = 0
17+
18+ left = 0
19+ right = n - 1
20+
21+ while left < right :
22+ curArea = abs (right - left ) * min (height [left ], height [right ])
23+ maxArea = max (curArea , maxArea )
24+
25+ #height ์ด ๋ฎ์์ชฝ pointer update
26+ if height [left ] < height [right ]:
27+ left += 1
28+ else :
29+ right -= 1
30+
31+ return maxArea
32+
Original file line number Diff line number Diff line change 1+ class TrieNode :
2+ def __init__ (self ):
3+ self .children = {}
4+ self .is_end = False
5+
6+ class WordDictionary :
7+ ''' ๋๋ฌด ์ด๋ ค์์ด ํ์ด ๋ดค์ด์..ใ
ใ
'''
8+
9+ def __init__ (self ):
10+ self .root = TrieNode ()
11+
12+ def addWord (self , word : str ) -> None :
13+ node = self .root
14+
15+ for ch in word :
16+ #node ์ ์กด์ฌํ์ง ์์ผ๋ฉด TrieNode ์์ฑ
17+ if ch not in node .children :
18+ node .children [ch ] = TrieNode ()
19+ #node ์ children ์ ์ฅ
20+ node = node .children [ch ]
21+ node .is_end = True
22+
23+ def dfs (self , node , word , i ):
24+ if i == len (word ):
25+ return node .is_end
26+
27+ #์ผ๋ฐ ๋ฌธ์์ผ ๊ฒฝ์ฐ
28+ ch = word [i ]
29+ if ch != '.' :
30+ if ch not in node .children :
31+ return False
32+ return self .dfs (node .children [ch ], word , i + 1 )
33+ #.์ด ํฌํจ๋ ๊ฒฝ์ฐ -> ๋ชจ๋ ๊ฒฝ์ฐ ํ์
34+ for child in node .children .values ():
35+ if self .dfs (child , word , i + 1 ):
36+ return True
37+ return False
38+
39+ def search (self , word : str ) -> bool :
40+ return self .dfs (self .root , word , 0 )
41+
42+
43+
44+ # Your WordDictionary object will be instantiated and called as such:
45+ # obj = WordDictionary()
46+ # obj.addWord(word)
47+ # param_2 = obj.search(word)
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ class Solution:
22 """
33 @param: strs: a list of strings
44 @return: encodes a list of strings to a single string.
5+ ๋ชจ๋ฅด๊ฒ ์ด์ ํด์ค์ ํ์ ๋น๋ ธ์ด์..ใ
56 """
67 def encode (self , strs : List [str ]):
78 text = ""
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def lengthOfLIS (self , nums : List [int ]) -> int :
3+ '''
4+ ๋ชจ๋ฅด๊ฒ ์ด์ ํ์ด๋ดค์ต๋๋ค ใ
ใ
5+ 1.problem: ์ฆ๊ฐํ๋ ๊ฐ์ฅ ๊ธด subsequence length return (์ต์ฅ ์ฆ๊ฐ ๋ถ๋ถ ์์ด)
6+ 2.์กฐ๊ฑด
7+ - nums array ๊ธธ์ด ์ต์ 1 , ์ต๋ 2500
8+ - ์์ ๊ฐ ์์ ๊ฐ๋ฅ
9+ 3.ํ์ด
10+ - dp : time complexity O(n^2)
11+ dp[i] = i๋ฒ์งธ ์์๋ฅผ ๋ง์ง๋ง์ผ๋กํ๋ LIS
12+ dp[i] max(dp[i], dp[j]+1) ๋ ๋ณด๋ค ์์ ์ ๋ค ์ค ๊ฐ์ฅ ๊ธด LIS + 1
13+ '''
14+
15+ n = len (nums )
16+ dp = [1 ] * (n )
17+
18+ for i in range (n ):
19+ for j in range (i ):
20+ #์ ์ซ์ nums[j]๊ฐ ์ง๊ธ nums[i]๋ณด๋ค ๋ ์์ ๊ฒฝ์ฐ dp[i] update
21+ if nums [j ] < nums [i ]:
22+ dp [i ] = max (dp [i ], dp [j ] + 1 )
23+ return max (dp )
24+
25+
26+
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def lengthOfLongestSubstring (self , s : str ) -> int :
3+ left = 0
4+ max_len = 0
5+ visited = set ()
6+
7+ for i in range (len (s )):
8+ while s [i ] in visited :
9+ visited .remove (s [left ])
10+ left += 1
11+
12+ visited .add (s [i ])
13+ max_len = max (max_len , i - left + 1 )
14+
15+ return max_len
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def numIslands (self , grid : List [List [str ]]) -> int :
3+ '''
4+ m = ์ธ๋ก(์ด), n = ๊ฐ๋ก(ํ)
5+ solution: dfs
6+ '''
7+ m = len (grid )
8+ n = len (grid [0 ])
9+ visited = [ [0 ] * n for _ in range (m )]
10+ count = 0
11+
12+ def dfs (i , j ):
13+ #๋ฒ์ ๋ฒ์ด๋๋ฉด return
14+ if i < 0 or i >= m or j < 0 or j >= n or grid [i ][j ] == "0" :
15+ return
16+
17+ if visited [i ][j ] == 1 :
18+ return
19+
20+ visited [i ][j ] = 1 #๋ฐฉ๋ฌธํ์
21+
22+ dfs (i + 1 , j )
23+ dfs (i - 1 , j )
24+ dfs (i , j + 1 )
25+ dfs (i , j - 1 )
26+
27+ for i in range (m ):
28+ for j in range (n ):
29+ if grid [i ][j ] == "1" and visited [i ][j ] == 0 :
30+ dfs (i ,j )
31+ count += 1
32+ return count
33+
Original file line number Diff line number Diff line change 1+ # Definition for singly-linked list.
2+ # class ListNode:
3+ # def __init__(self, val=0, next=None):
4+ # self.val = val
5+ # self.next = next
6+ class Solution :
7+ def reverseList (self , head : Optional [ListNode ]) -> Optional [ListNode ]:
8+
9+ if head is None :
10+ return None
11+
12+ st = []
13+ cur = head
14+
15+ while cur :
16+ st .append (cur .val )
17+ cur = cur .next
18+
19+ dummy = ListNode (0 )
20+ cur = dummy
21+
22+ while st :
23+ cur_value = st .pop ()
24+ cur .next = ListNode (cur_value )
25+ cur = cur .next
26+
27+ return dummy .next
28+
29+
30+
31+
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def spiralOrder (self , matrix : List [List [int ]]) -> List [int ]:
3+ '''
4+ 1.๋ฌธ์ : ๋์ ํ์ผ๋ก numbers return
5+ 2.์กฐ๊ฑด
6+ - m, n ๊ธธ์ด ์ต์ = 1. ์ต๋ = 10
7+ - ์์ ๊ฐ ์ต์ = -100, ์ต๋ = 100
8+ 3.ํ์ด
9+ - ๋ง์ง๋ง ์ปฌ๋ผ์ ์ค๋ฉด index j change
10+ '''
11+
12+ if len (matrix ) == 1 and len (matrix [0 ]) == 1 :
13+ return [matrix [0 ][0 ]]
14+
15+ left = 0
16+ right = len (matrix [0 ]) - 1
17+ top = 0
18+ bottom = len (matrix ) - 1
19+ result = []
20+
21+ while top <= bottom and left <= right :
22+ #left -> right
23+ for i in range (left , right + 1 ):
24+ result .append (matrix [top ][i ])
25+ top += 1
26+
27+ #top -> bottom
28+ for i in range (top , bottom + 1 ):
29+ result .append (matrix [i ][right ])
30+ right -= 1
31+
32+ #right -> left
33+ if top <= bottom :
34+ for i in range (right , left - 1 , - 1 ):
35+ result .append (matrix [bottom ][i ])
36+ bottom -= 1
37+
38+ #bottom -> top
39+ if left <= right :
40+ for i in range (bottom , top - 1 , - 1 ):
41+ result .append (matrix [i ][left ])
42+ left += 1
43+ return result
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def isValid (self , s : str ) -> bool :
3+ '''
4+ ๋ฌธ์ : string s ์ ๋ํด์ valid parentheses ์ด๋ฉด true ์๋๋ฉด false
5+ conditions
6+ - open brackets must be closed by the same type
7+ - // must be closed in the correct order
8+ - s ์ต์ ๊ธธ์ด = 1, ์ต๋ 10^4
9+ solution
10+ - open brackets -> st array ์ ์ ์ฅ, close brackets -> st.pop -> check valid
11+ - ๋ง์ง๋ง์ st array length ๊ฐ 1 ์ด์์ด๋ฉด return False
12+
13+ - time complexity: O(n)
14+ - space complexity: O(n)
15+ '''
16+
17+ st = []
18+
19+ for i in range (len (s )):
20+ if s [i ] == '(' or s [i ] == '{' or s [i ] == '[' :
21+ st .append (s [i ])
22+ else :
23+ # check if st is empty
24+ if len (st ) <= 0 :
25+ return False
26+ cur = st .pop ()
27+ if s [i ] == ')' and cur != '(' :
28+ return False
29+ if s [i ] == '}' and cur != '{' :
30+ return False
31+ if s [i ] == ']' and cur != '[' :
32+ return False
33+ if len (st ) > 0 :
34+ return False
35+ return True
36+
37+
You canโt perform that action at this time.
0 commit comments