-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #936 from yolophg/main
[Helena] Week 7
- Loading branch information
Showing
5 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Time Complexity: O(n): both pointers (left and right) traverse the string once. | ||
# Space Complexity: O(n): worst case, all characters are unique, so the set will store all characters. | ||
|
||
class Solution: | ||
def lengthOfLongestSubstring(self, s: str) -> int: | ||
# to store characters in the current window | ||
window_set = set() | ||
# to store the max length of a substring | ||
max_length = 0 | ||
# left pointer for the sliding window | ||
left = 0 | ||
|
||
# iterate through each char in the string using the right pointer | ||
for right in range(len(s)): | ||
# if the char is already in the window, shrink the window | ||
while s[right] in window_set: | ||
window_set.remove(s[left]) # remove the leftmost char | ||
left += 1 # move the left pointer to the right | ||
# add the new char | ||
window_set.add(s[right]) | ||
# update the max length if the current window is longer | ||
max_length = max(max_length, right - left + 1) | ||
|
||
return max_length |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Time Complexity: O(N * M), where N is the number of rows and M is the number of columns. | ||
# Space Complexity: O(N * M), in the worst case if the grid is entirely land. | ||
|
||
class Solution: | ||
def numIslands(self, grid: List[List[str]]) -> int: | ||
# get the number of rows and columns in the grid | ||
rows, cols = len(grid), len(grid[0]) | ||
|
||
# define a dfs function to mark visited cells | ||
def dfs(i, j): | ||
# if out of bounds or the cell is water, stop here | ||
if i < 0 or i >= rows or j < 0 or j >= cols or grid[i][j] == "0": | ||
return True | ||
|
||
# mark the current cell as visited by changing "1" to "0" | ||
grid[i][j] = "0" | ||
|
||
# visit recursively all neighboring cells | ||
return True if (dfs(i - 1, j) and dfs(i + 1, j) and dfs(i, j - 1) and dfs(i, j + 1)) else False | ||
|
||
count = 0 | ||
# loop through the entire grid to find islands | ||
for i in range(rows): | ||
for j in range(cols): | ||
# if we find land ("1"), start a dfs | ||
if grid[i][j] == "1": | ||
# increment count if dfs confirms a new island | ||
if dfs(i, j): | ||
count += 1 | ||
return count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
|
||
class Solution: | ||
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
prev = None | ||
# to traverse the original list, starting from head | ||
current = head | ||
while current: | ||
# reverse the link and move to the next node | ||
prev, prev.next, current = current, prev, current.next | ||
|
||
# prev is now the head of the reversed list | ||
return prev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Time Complexity: O(m * n) - iterate through the matrix multiple times. | ||
# Space Complexity: O(1) - no extra space is used apart from variables. | ||
|
||
class Solution: | ||
def setZeroes(self, matrix: List[List[int]]) -> None: | ||
m, n = len(matrix), len(matrix[0]) | ||
|
||
# check if the first row contains any zero | ||
first_row_zero = any(matrix[0][j] == 0 for j in range(n)) | ||
# check if the first column contains any zero | ||
first_col_zero = any(matrix[i][0] == 0 for i in range(m)) | ||
|
||
# use the first row and column to mark zero | ||
for i in range(1, m): | ||
for j in range(1, n): | ||
if matrix[i][j] == 0: | ||
matrix[i][0] = 0 | ||
matrix[0][j] = 0 | ||
|
||
# update the matrix using the marks from the first row and column | ||
for i in range(1, m): | ||
for j in range(1, n): | ||
if matrix[i][0] == 0 or matrix[0][j] == 0: | ||
matrix[i][j] = 0 | ||
|
||
# handle the first row separately if it initially had any zero | ||
if first_row_zero: | ||
for j in range(n): | ||
matrix[0][j] = 0 | ||
|
||
# handle the first column separately if it initially had any zero | ||
if first_col_zero: | ||
for i in range(m): | ||
matrix[i][0] = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Time Complexity: O(N * M) : iterate through a grid of size m x n once. | ||
# Space Complexity: O(N * M) : use a DP table of size (m+1) x (n+1) to store the number of paths. | ||
|
||
class Solution: | ||
def uniquePaths(self, m: int, n: int) -> int: | ||
# create a dp table and add extra rows and columns for easier indexing | ||
table = [[0 for x in range(n+1)] for y in range(m+1)] | ||
|
||
# set the starting point, one way to be at the start | ||
table[1][1] = 1 | ||
|
||
# iterate the grid to calculate paths | ||
for i in range(1, m+1): | ||
for j in range(1, n+1): | ||
# add paths going down | ||
if i+1 <= m: | ||
table[i+1][j] += table[i][j] | ||
# add paths going right | ||
if j+1 <= n: | ||
table[i][j+1] += table[i][j] | ||
|
||
return table[m][n] |