Skip to content

Commit eb43e21

Browse files
authored
Merge pull request #936 from yolophg/main
[Helena] Week 7
2 parents 9f2e250 + 529ab2e commit eb43e21

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Time Complexity: O(n): both pointers (left and right) traverse the string once.
2+
# Space Complexity: O(n): worst case, all characters are unique, so the set will store all characters.
3+
4+
class Solution:
5+
def lengthOfLongestSubstring(self, s: str) -> int:
6+
# to store characters in the current window
7+
window_set = set()
8+
# to store the max length of a substring
9+
max_length = 0
10+
# left pointer for the sliding window
11+
left = 0
12+
13+
# iterate through each char in the string using the right pointer
14+
for right in range(len(s)):
15+
# if the char is already in the window, shrink the window
16+
while s[right] in window_set:
17+
window_set.remove(s[left]) # remove the leftmost char
18+
left += 1 # move the left pointer to the right
19+
# add the new char
20+
window_set.add(s[right])
21+
# update the max length if the current window is longer
22+
max_length = max(max_length, right - left + 1)
23+
24+
return max_length

number-of-islands/yolophg.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Time Complexity: O(N * M), where N is the number of rows and M is the number of columns.
2+
# Space Complexity: O(N * M), in the worst case if the grid is entirely land.
3+
4+
class Solution:
5+
def numIslands(self, grid: List[List[str]]) -> int:
6+
# get the number of rows and columns in the grid
7+
rows, cols = len(grid), len(grid[0])
8+
9+
# define a dfs function to mark visited cells
10+
def dfs(i, j):
11+
# if out of bounds or the cell is water, stop here
12+
if i < 0 or i >= rows or j < 0 or j >= cols or grid[i][j] == "0":
13+
return True
14+
15+
# mark the current cell as visited by changing "1" to "0"
16+
grid[i][j] = "0"
17+
18+
# visit recursively all neighboring cells
19+
return True if (dfs(i - 1, j) and dfs(i + 1, j) and dfs(i, j - 1) and dfs(i, j + 1)) else False
20+
21+
count = 0
22+
# loop through the entire grid to find islands
23+
for i in range(rows):
24+
for j in range(cols):
25+
# if we find land ("1"), start a dfs
26+
if grid[i][j] == "1":
27+
# increment count if dfs confirms a new island
28+
if dfs(i, j):
29+
count += 1
30+
return count

reverse-linked-list/yolophg.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Time Complexity: O(n)
2+
# Space Complexity: O(1)
3+
4+
class Solution:
5+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
6+
prev = None
7+
# to traverse the original list, starting from head
8+
current = head
9+
while current:
10+
# reverse the link and move to the next node
11+
prev, prev.next, current = current, prev, current.next
12+
13+
# prev is now the head of the reversed list
14+
return prev

set-matrix-zeroes/yolophg.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Time Complexity: O(m * n) - iterate through the matrix multiple times.
2+
# Space Complexity: O(1) - no extra space is used apart from variables.
3+
4+
class Solution:
5+
def setZeroes(self, matrix: List[List[int]]) -> None:
6+
m, n = len(matrix), len(matrix[0])
7+
8+
# check if the first row contains any zero
9+
first_row_zero = any(matrix[0][j] == 0 for j in range(n))
10+
# check if the first column contains any zero
11+
first_col_zero = any(matrix[i][0] == 0 for i in range(m))
12+
13+
# use the first row and column to mark zero
14+
for i in range(1, m):
15+
for j in range(1, n):
16+
if matrix[i][j] == 0:
17+
matrix[i][0] = 0
18+
matrix[0][j] = 0
19+
20+
# update the matrix using the marks from the first row and column
21+
for i in range(1, m):
22+
for j in range(1, n):
23+
if matrix[i][0] == 0 or matrix[0][j] == 0:
24+
matrix[i][j] = 0
25+
26+
# handle the first row separately if it initially had any zero
27+
if first_row_zero:
28+
for j in range(n):
29+
matrix[0][j] = 0
30+
31+
# handle the first column separately if it initially had any zero
32+
if first_col_zero:
33+
for i in range(m):
34+
matrix[i][0] = 0

unique-paths/yolophg.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Time Complexity: O(N * M) : iterate through a grid of size m x n once.
2+
# Space Complexity: O(N * M) : use a DP table of size (m+1) x (n+1) to store the number of paths.
3+
4+
class Solution:
5+
def uniquePaths(self, m: int, n: int) -> int:
6+
# create a dp table and add extra rows and columns for easier indexing
7+
table = [[0 for x in range(n+1)] for y in range(m+1)]
8+
9+
# set the starting point, one way to be at the start
10+
table[1][1] = 1
11+
12+
# iterate the grid to calculate paths
13+
for i in range(1, m+1):
14+
for j in range(1, n+1):
15+
# add paths going down
16+
if i+1 <= m:
17+
table[i+1][j] += table[i][j]
18+
# add paths going right
19+
if j+1 <= n:
20+
table[i][j+1] += table[i][j]
21+
22+
return table[m][n]

0 commit comments

Comments
 (0)