File tree Expand file tree Collapse file tree 5 files changed +139
-0
lines changed
find-minimum-in-rotated-sorted-array
pacific-atlantic-water-flow Expand file tree Collapse file tree 5 files changed +139
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ # 시간복잡도: O(logN)
3
+ # 공간복잡도: O(1)
4
+ def findMin (self , nums : List [int ]) -> int :
5
+ n = len (nums )
6
+ st , en = 0 , n - 1
7
+ while st < en :
8
+ mid = (st + en )// 2
9
+ if nums [mid ] > nums [en ]:
10
+ st = mid + 1
11
+ else :
12
+ en = mid
13
+
14
+ return nums [st ]
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ # 시간복잡도: O(N)
3
+ # 공간복잡도: O(N)
4
+ def hasCycle (self , head : Optional [ListNode ]) -> bool :
5
+
6
+ visited = set ()
7
+ while head :
8
+ if head in visited :
9
+ return True
10
+
11
+ visited .add (head )
12
+ head = head .next
13
+
14
+ return False
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ # 시간복잡도: O(N)
3
+ # 공간복잡도: O(1)
4
+ def maxSubArray (self , nums : List [int ]) -> int :
5
+
6
+ prev = 0
7
+ answer = float ('-inf' )
8
+ for num in nums :
9
+ if prev + num > num :
10
+ prev += num
11
+ else :
12
+ prev = num
13
+ answer = max (answer , prev )
14
+
15
+ return max (prev , answer )
Original file line number Diff line number Diff line change
1
+ from collections import Counter , deque
2
+ class Solution :
3
+ # 시간복잡도: O(S+T)
4
+ # 공간복잡도: O(T)
5
+ def minWindow (self , s : str , t : str ) -> str :
6
+ counter = Counter (t )
7
+
8
+ index = deque ()
9
+ tot = 0
10
+ m = len (s )
11
+ st , en = 0 , m - 1
12
+ for idx , ch in enumerate (s ):
13
+ if ch not in counter :
14
+ continue
15
+
16
+ counter [ch ] -= 1
17
+ index .append (idx )
18
+
19
+ if counter [ch ] == 0 :
20
+ tot += 1
21
+
22
+ while index :
23
+ if counter [s [index [0 ]]] < 0 :
24
+ counter [s [index [0 ]]] += 1
25
+ index .popleft ()
26
+ else :
27
+ break
28
+
29
+ if tot == len (counter ):
30
+ a = index [0 ]
31
+ b = idx
32
+ if b - a + 1 < m :
33
+ st , en = a , b
34
+ m = en - st + 1
35
+
36
+ if tot != len (counter ):
37
+ return ""
38
+
39
+ return s [st :en + 1 ]
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ # 시간복잡도: O(N*M)
3
+ # 공간복잡도: O(N*M)
4
+ def pacificAtlantic (self , heights : List [List [int ]]) -> List [List [int ]]:
5
+
6
+ m = len (heights )
7
+ n = len (heights [0 ])
8
+ dx = [- 1 , 1 , 0 , 0 ]
9
+ dy = [0 , 0 , - 1 , 1 ]
10
+
11
+ dp = [[False ] * (n ) for _ in range (m )]
12
+ dp [0 ][n - 1 ] = True
13
+ dp [m - 1 ][0 ] = True
14
+ visited = set ()
15
+
16
+ def dfs (x , y ):
17
+ if dp [x ][y ]:
18
+ return 2
19
+
20
+ check = set ()
21
+ for i in range (4 ):
22
+ nx = x + dx [i ]
23
+ ny = y + dy [i ]
24
+
25
+ if 0 <= nx < m and 0 <= ny < n :
26
+ if (nx , ny ) not in visited and heights [nx ][ny ] <= heights [x ][y ]:
27
+ visited .add ((nx , ny ))
28
+ res = dfs (nx , ny )
29
+
30
+ if res != - 1 :
31
+ check .add (res )
32
+ visited .remove ((nx , ny ))
33
+ else :
34
+ if x == 0 or y == 0 :
35
+ check .add (0 )
36
+ if x == m - 1 or y == n - 1 :
37
+ check .add (1 )
38
+
39
+ if 2 in check :
40
+ dp [x ][y ] = 2
41
+ return 2
42
+
43
+ if len (check ) == 2 :
44
+ return 2
45
+
46
+ if check :
47
+ return check .pop ()
48
+
49
+ return - 1
50
+
51
+ answer = []
52
+ for i in range (m ):
53
+ for j in range (n ):
54
+ if dfs (i , j ) == 2 :
55
+ answer .append ([i , j ])
56
+
57
+ return answer
You can’t perform that action at this time.
0 commit comments