Skip to content

Commit 9f2e250

Browse files
authored
Merge pull request #935 from dusunax/main
[SunaDu] - Week 7
2 parents 82f5fbc + 195d9b9 commit 9f2e250

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'''
2+
# 3. Longest Substring Without Repeating Characters
3+
4+
use a set to store the characters in the current substring.
5+
6+
7+
## Time and Space Complexity
8+
9+
### Solution 1: using set
10+
11+
```
12+
TC: O(n)
13+
SC: O(n)
14+
```
15+
16+
#### TC is O(n):
17+
- iterating with end pointer through the string once. = O(n)
18+
- inner while loop runs at most once for each character in the string. = Amortized O(1)
19+
20+
#### SC is O(n):
21+
- using a set to store the characters in the current substring. = O(n)
22+
23+
### Solution 2: using ASCII array
24+
25+
```
26+
TC: O(n)
27+
SC: O(128)
28+
```
29+
30+
#### TC is O(n):
31+
- iterating with end pointer through the string once. = O(n)
32+
- checking if the character is in the current substring.
33+
34+
#### SC is O(1):
35+
- using an array to store the characters in the current substring. = constant space O(128)
36+
'''
37+
class Solution:
38+
def lengthOfLongestSubstringWithSet(self, s: str) -> int:
39+
max_count = 0
40+
start = 0
41+
substrings = set() # SC: O(n)
42+
43+
for end in range(len(s)): # TC: O(n)
44+
while s[end] in substrings: # TC: Amortized O(1)
45+
substrings.remove(s[start])
46+
start += 1
47+
substrings.add(s[end])
48+
max_count = max(max_count, end - start + 1)
49+
50+
return max_count
51+
52+
def lengthOfLongestSubstring(self, s: str) -> int:
53+
max_count = 0
54+
start = 0
55+
char_index = [-1] * 128 # SC: O(128)
56+
57+
for end in range(len(s)): # TC: O(n)
58+
if char_index[ord(s[end])] >= start:
59+
start = char_index[ord(s[end])] + 1
60+
char_index[ord(s[end])] = end
61+
max_count = max(max_count, end - start + 1)
62+
63+
return max_count

number-of-islands/dusunax.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
# 200. Number of Islands
3+
4+
use DFS to find the number of islands (to find the all the possible cases)
5+
6+
## Time and Space Complexity
7+
8+
```
9+
TC: O(m * n)
10+
SC: O(m * n)
11+
```
12+
13+
### TC is O(m * n):
14+
- dfs function is called for each cell in the grid for checking is the land ("1") = O(m * n)
15+
16+
### SC is O(m * n):
17+
- using a recursive function, the call stack can go as deep as the number of cells in the grid in the worst case. = O(m * n)
18+
'''
19+
class Solution:
20+
def numIslands(self, grid: List[List[str]]) -> int:
21+
def dfs(x, y):
22+
if x < 0 or y < 0 or y >= len(grid) or x >= len(grid[0]) or grid[y][x] == "0" :
23+
return
24+
25+
grid[y][x] = "0"
26+
dfs(x, y + 1)
27+
dfs(x - 1, y)
28+
dfs(x, y - 1)
29+
dfs(x + 1, y)
30+
31+
island_count = 0
32+
for y in range(len(grid)):
33+
for x in range(len(grid[0])):
34+
if grid[y][x] == "1":
35+
dfs(x, y)
36+
island_count += 1
37+
38+
return island_count

reverse-linked-list/dusunax.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
# 206. Reverse Linked List
3+
4+
iterate through the linked list and reverse the direction of the pointers.
5+
6+
## Time and Space Complexity
7+
8+
```
9+
TC: O(n)
10+
SC: O(1)
11+
```
12+
'''
13+
class Solution:
14+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
15+
prev = None
16+
current = head
17+
18+
while current is not None: # TC: O(n)
19+
next_list_temp = current.next
20+
current.next = prev
21+
prev = current
22+
current = next_list_temp
23+
24+
return prev

set-matrix-zeroes/dusunax.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'''
2+
# 73. Set Matrix Zeroes
3+
# solution reference: https://www.algodale.com/problems/set-matrix-zeroes/
4+
'''
5+
class Solution:
6+
'''
7+
### TC is O(m * n):
8+
- iterating through every cells, to find the zero cells. = O(m * n) 1️⃣
9+
- iterating through every cells, to update the rows. = O(m * n) 2️⃣
10+
- iterating through every cells, to update the columns. = O(m * n) 3️⃣
11+
12+
### SC is O(m + n):
13+
- using each set to store the rows(O(m)) and columns(O(n)) that have zero. = O(m + n)
14+
'''
15+
def setZeroesWithSet(self, matrix: List[List[int]]) -> None:
16+
zero_rows = set() # SC: O(m)
17+
zero_cols = set() # SC: O(n)
18+
19+
for r in range(len(matrix)): # TC: O(m * n)
20+
for c in range(len(matrix[0])):
21+
if matrix[r][c] == 0:
22+
zero_rows.add(r)
23+
zero_cols.add(c)
24+
25+
for r in zero_rows: # TC: O(m * n)
26+
for i in range(len(matrix[0])):
27+
matrix[r][i] = 0
28+
29+
for c in zero_cols: # TC: O(m * n)
30+
for i in range(len(matrix)):
31+
matrix[i][c] = 0
32+
33+
'''
34+
### TC is O(m * n):
35+
- check if the first row or column has zero. = O(m + n)
36+
- iterating through every cells, if it has zero, mark the first row and column. = O(m * n) 1️⃣
37+
- update the matrix based on the marks(0) in the first row and column. = O(m * n) 2️⃣
38+
- if the first row or column has zero, iterating through every cells, in the first row or column and updating it. = O(m + n)
39+
40+
### SC is O(1):
41+
- using the first_row_has_zero and first_col_has_zero to store the zero information. = O(1)
42+
'''
43+
def setZeroesWithMarkerAndVariable(self, matrix: List[List[int]]) -> None:
44+
rows = len(matrix)
45+
cols = len(matrix[0])
46+
47+
first_row_has_zero = any(matrix[0][j] == 0 for j in range(cols)) # TC: O(n), SC: O(1)
48+
first_col_has_zero = any(matrix[i][0] == 0 for i in range(rows)) # TC: O(m), SC: O(1)
49+
50+
for r in range(1, rows): # TC: O(m * n)
51+
for c in range(1, cols):
52+
if matrix[r][c] == 0:
53+
matrix[r][0] = 0
54+
matrix[0][c] = 0
55+
56+
for r in range(1, rows): # TC: O(m * n)
57+
for c in range(1, cols):
58+
if matrix[r][0] == 0 or matrix[0][c] == 0:
59+
matrix[r][c] = 0
60+
61+
if first_row_has_zero:
62+
for c in range(cols): # TC: O(n)
63+
matrix[0][c] = 0
64+
65+
if first_col_has_zero:
66+
for r in range(rows): # TC: O(m)
67+
matrix[r][0] = 0

unique-paths/dusunax.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
# 62. Unique Paths
3+
4+
use dynamic programming & a dp table to store the number of ways to reach each cell.
5+
6+
### TC is O(m * n):
7+
- iterating through every cell in the grid. = O(m * n)
8+
- updating each cell in the grid. = O(1)
9+
10+
### SC is O(m * n):
11+
- using a dp table (2D array) to store the number of ways to reach each cell. = O(m * n)
12+
'''
13+
class Solution:
14+
def uniquePaths(self, m: int, n: int) -> int:
15+
dp = [[1] * n for _ in range(m)]
16+
17+
for y in range(1, m):
18+
for x in range(1, n):
19+
dp[y][x] = dp[y - 1][x] + dp[y][x - 1]
20+
21+
return dp[m - 1][n - 1]

0 commit comments

Comments
 (0)