File tree Expand file tree Collapse file tree 5 files changed +138
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 5 files changed +138
-0
lines changed Original file line number Diff line number Diff line change
1
+ from collections import deque
2
+
3
+ class Solution :
4
+ """
5
+ Time complexity O(n)
6
+ Space complexity O(n)
7
+ """
8
+ def lengthOfLongestSubstring (self , s : str ) -> int :
9
+ max_len = 0
10
+ q = deque ()
11
+ for i , ch in enumerate (s ):
12
+ if ch not in q :
13
+ q .append (ch )
14
+ if len (q ) > max_len :
15
+ max_len = len (q )
16
+ else :
17
+ while True :
18
+ tmp = q .popleft ()
19
+ if tmp == ch :
20
+ break
21
+ q .append (ch )
22
+
23
+ return max_len
24
+
25
+ def slidingWindow (self , s ):
26
+ start , end = 0 , 0
27
+ substr = set ()
28
+ max_len = 0
29
+ while end < len (s ):
30
+ if s [end ] in substr :
31
+ substr .remove (s [start ])
32
+ start += 1
33
+ else :
34
+ substr .add (s [end ])
35
+ end += 1
36
+ max_len = max (end - start , max_len )
37
+ return max_len
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ """
3
+ Time, Space comlexity O(n*m)
4
+
5
+ connected components
6
+ dfs, bfs
7
+ """
8
+ def numIslands (self , grid : List [List [str ]]) -> int :
9
+ n , m = len (grid ), len (grid [0 ])
10
+ visited = [[False for _ in range (m )] for _ in range (n )] # visited 대신 grid를 0으로 표시할수도 있다
11
+ islands = 0
12
+
13
+ def dfs (x , y ):
14
+ stack = [(x , y )]
15
+ while stack :
16
+ x , y = stack .pop ()
17
+ dx = [- 1 , 1 , 0 , 0 ]
18
+ dy = [0 , 0 , - 1 , 1 ]
19
+ for k in range (4 ):
20
+ nx , ny = x + dx [k ], y + dy [k ]
21
+ if 0 <= nx <= n - 1 and 0 <= ny <= m - 1 :
22
+ if not visited [nx ][ny ] and grid [nx ][ny ] == "1" :
23
+ visited [nx ][ny ] = True
24
+ stack .append ((nx , ny ))
25
+
26
+ for i in range (n ):
27
+ for j in range (m ):
28
+ if not visited [i ][j ] and grid [i ][j ] == "1" :
29
+ dfs (i , j )
30
+ islands += 1
31
+
32
+ return islands
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
+ """
8
+ Time complexity O(n)
9
+ Space complexity O(1)
10
+ """
11
+ def reverseList (self , head : Optional [ListNode ]) -> Optional [ListNode ]:
12
+ if not head or not head .next :
13
+ return head
14
+
15
+ node = head .next
16
+ prev = head
17
+ prev .next = None
18
+
19
+ while node :
20
+ next_node = node .next
21
+ node .next = prev
22
+ prev = node
23
+ node = next_node
24
+
25
+ return prev
Original file line number Diff line number Diff line change
1
+ """
2
+ Time, space complexity O(m * n)
3
+ """
4
+
5
+ class Solution :
6
+ def setZeroes (self , matrix : List [List [int ]]) -> None :
7
+ """
8
+ Do not return anything, modify matrix in-place instead.
9
+ """
10
+ m , n = len (matrix ), len (matrix [0 ])
11
+ i_indices = set ()
12
+ j_indices = set ()
13
+ for i in range (m ):
14
+ for j in range (n ):
15
+ if matrix [i ][j ] == 0 :
16
+ i_indices .add (i )
17
+ j_indices .add (j )
18
+
19
+ for i in i_indices :
20
+ matrix [i ] = [0 for _ in range (n )]
21
+ for j in j_indices :
22
+ for i in range (m ):
23
+ matrix [i ][j ] = 0
Original file line number Diff line number Diff line change
1
+ """
2
+ Time, space complexity O(m*n)
3
+
4
+ Dynamic programming
5
+ """
6
+
7
+ class Solution :
8
+ def uniquePaths (self , m : int , n : int ) -> int :
9
+ dp = [[0 for _ in range (n )] for _ in range (m )]
10
+ dp [0 ] = [1 for _ in range (n )]
11
+
12
+ for i in range (1 , m ):
13
+ for j in range (n ):
14
+ if i == 0 :
15
+ continue
16
+ if j == 0 :
17
+ dp [i ][j ] = 1
18
+ continue
19
+ dp [i ][j ] = dp [i - 1 ][j ] + dp [i ][j - 1 ]
20
+
21
+ return dp [- 1 ][- 1 ]
You can’t perform that action at this time.
0 commit comments