File tree 5 files changed +150
-0
lines changed
longest-substring-without-repeating-characters
5 files changed +150
-0
lines changed Original file line number Diff line number Diff line change
1
+ '''
2
+ μκ°λ³΅μ‘λ: O(n)
3
+ - rightμ left ν¬μΈν°κ° κ°κ° μ΅λ nλ² μμ§μ
4
+
5
+ 곡κ°λ³΅μ‘λ: O(n)
6
+ - hars μ§ν©μ΄ μ΅λ nκ°μ κ³ μ λ¬Έμλ₯Ό μ μ₯ν μ μ
7
+ '''
8
+
9
+ class Solution :
10
+ def lengthOfLongestSubstring (self , s : str ) -> int :
11
+ length = 0
12
+ left = 0
13
+ chars = set ()
14
+
15
+ for right in range (len (s )):
16
+ if s [right ] in chars :
17
+ while s [right ] in chars :
18
+ chars .remove (s [left ])
19
+ left += 1
20
+ chars .add (s [right ])
21
+ length = max (length , right + 1 - left )
22
+
23
+ return length
Original file line number Diff line number Diff line change
1
+ '''
2
+ μκ° λ³΅μ‘λ: O(m * n)
3
+ - κ° μ
μ ν λ²μ© λ°©λ¬Ένλ©°, DFSλ₯Ό ν΅ν΄ μ°κ²°λ λͺ¨λ μ
μ νμΈνλ―λ‘ gridμ λͺ¨λ μ
μ λν΄ O(1) μμ
μ΄ μνλ©λλ€.
4
+
5
+ κ³΅κ° λ³΅μ‘λ: O(m * n)
6
+ - μ΅μ
μ κ²½μ°, DFSμ νΈμΆ μ€νμ΄ κ²©μμ λͺ¨λ μ
μ λν΄ μμΌ μ μμ΅λλ€. μ΄ κ²½μ° μ€νμ μ μ₯λλ μ
μ κ°μλ μ΅λ m * n μ
λλ€.
7
+ '''
8
+
9
+ from typing import List
10
+
11
+
12
+ class Solution :
13
+ def numIslands (self , grid : List [List [str ]]) -> int :
14
+ count = 0
15
+ m , n = len (grid ), len (grid [0 ])
16
+
17
+ def exploreIsland (row : int , col : int ):
18
+ withinBounds = (0 <= row < m ) and (0 <= col < n )
19
+ if not withinBounds or grid [row ][col ] != "1" :
20
+ return
21
+
22
+ grid [row ][col ] = "#"
23
+
24
+ exploreIsland (row + 1 , col )
25
+ exploreIsland (row - 1 , col )
26
+ exploreIsland (row , col + 1 )
27
+ exploreIsland (row , col - 1 )
28
+
29
+ # Iterate through all cells in the grid
30
+ for i in range (m ):
31
+ for j in range (n ):
32
+ # if a cell is "1", increment count and mark the whole island as "#"
33
+ if grid [i ][j ] == "1" :
34
+ count += 1
35
+ exploreIsland (i , j )
36
+
37
+ return count
Original file line number Diff line number Diff line change
1
+ '''
2
+ μκ° λ³΅μ‘λ: O(n)
3
+ - 리μ€νΈμ λͺ¨λ λ
Έλλ₯Ό ν λ²μ© λ°©λ¬Ένλ―λ‘ μκ° λ³΅μ‘λλ O(n)μ
λλ€.
4
+
5
+ κ³΅κ° λ³΅μ‘λ: O(n)
6
+ - μ¬κ· νΈμΆμ μ¬μ©νλ―λ‘ νΈμΆ μ€νμ μ΅λ nλ²μ ν¨μ νΈμΆμ΄ μμ΄λ―λ‘ κ³΅κ° λ³΅μ‘λλ O(n)μ
λλ€.
7
+ '''
8
+ from typing import Optional
9
+
10
+ class ListNode :
11
+ def __init__ (self , val = 0 , next = None ):
12
+ self .val = val
13
+ self .next = next
14
+
15
+ class Solution :
16
+ def reverseList (self , head : Optional [ListNode ]) -> Optional [ListNode ]:
17
+ if not head or not head .next :
18
+ return head
19
+
20
+ new_head = self .reverseList (head .next ) # find the last node
21
+ head .next .next = head # reverse
22
+ head .next = None # remove cycle
23
+
24
+ return new_head
Original file line number Diff line number Diff line change
1
+ '''
2
+ μκ°λ³΅μ‘λ: O(m * n)
3
+ 곡κ°λ³΅μ‘λ: O(1)
4
+ '''
5
+
6
+ from typing import List
7
+
8
+
9
+ class Solution :
10
+ def setZeroes (self , matrix : List [List [int ]]) -> None :
11
+ m , n = len (matrix ), len (matrix [0 ])
12
+ first_row_zero = False # Flag to check if the first row needs to be zeroed
13
+ first_col_zero = False # Flag to check if the first column needs to be zeroed
14
+
15
+ # Check if the first row has any zeros
16
+ for j in range (n ):
17
+ if matrix [0 ][j ] == 0 :
18
+ first_row_zero = True
19
+ break
20
+
21
+ # Check if the first column has any zeros
22
+ for i in range (m ):
23
+ if matrix [i ][0 ] == 0 :
24
+ first_col_zero = True
25
+ break
26
+
27
+ # Use the first row and column to mark rows and columns that need to be zeroed
28
+ for i in range (1 , m ):
29
+ for j in range (1 , n ):
30
+ if matrix [i ][j ] == 0 :
31
+ matrix [i ][0 ] = 0
32
+ matrix [0 ][j ] = 0
33
+
34
+ # Zero out cells based on markers in the first row and column
35
+ for i in range (1 , m ):
36
+ for j in range (1 , n ):
37
+ if matrix [i ][0 ] == 0 or matrix [0 ][j ] == 0 :
38
+ matrix [i ][j ] = 0
39
+
40
+ # Zero out the first row if needed
41
+ if first_row_zero :
42
+ for j in range (n ):
43
+ matrix [0 ][j ] = 0
44
+
45
+ # Zero out the first column if needed
46
+ if first_col_zero :
47
+ for i in range (m ):
48
+ matrix [i ][0 ] = 0
Original file line number Diff line number Diff line change
1
+ '''
2
+ μκ° λ³΅μ‘λ: O(m * n)
3
+ - λμ νλ‘κ·Έλλ° ν
μ΄λΈ(dp)μ μ¬μ©νμ¬ κ° μ
μμμ κ²½λ‘ μλ₯Ό ν λ²μ© κ³μ°νλ―λ‘ μκ° λ³΅μ‘λλ 격μμ λͺ¨λ μ
μ λν΄ O(m * n)μ
λλ€.
4
+
5
+ κ³΅κ° λ³΅μ‘λ: O(m * n)
6
+ - dp ν
μ΄λΈμ μ¬μ©νμ¬ λͺ¨λ μ
μ λν κ²½λ‘ μλ₯Ό μ μ₯νλ―λ‘ κ³΅κ° λ³΅μ‘λλ O(m * n)μ
λλ€.
7
+ '''
8
+
9
+ class Solution :
10
+ def uniquePaths (self , m : int , n : int ) -> int :
11
+ # save number of unique paths to each cell
12
+ dp = [[1 ] * n for _ in range (m )]
13
+
14
+ for row in range (1 , m ):
15
+ for col in range (1 , n ):
16
+ dp [row ][col ] = dp [row ][col - 1 ] + dp [row - 1 ][col ]
17
+
18
+ return dp [m - 1 ][n - 1 ]
You canβt perform that action at this time.
0 commit comments