Skip to content

[SunaDu] Week 7 #935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions longest-substring-without-repeating-characters/dusunax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'''
# 3. Longest Substring Without Repeating Characters

use a set to store the characters in the current substring.


## Time and Space Complexity

### Solution 1: using set

```
TC: O(n)
SC: O(n)
```

#### TC is O(n):
- iterating with end pointer through the string once. = O(n)
- inner while loop runs at most once for each character in the string. = Amortized O(1)

#### SC is O(n):
- using a set to store the characters in the current substring. = O(n)

### Solution 2: using ASCII array

```
TC: O(n)
SC: O(128)
```

#### TC is O(n):
- iterating with end pointer through the string once. = O(n)
- checking if the character is in the current substring.

#### SC is O(1):
- using an array to store the characters in the current substring. = constant space O(128)
'''
class Solution:
def lengthOfLongestSubstringWithSet(self, s: str) -> int:
max_count = 0
start = 0
substrings = set() # SC: O(n)
Copy link
Contributor

@ekgns33 ekgns33 Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

부분문자열에서 각 문자는 1회만 등장할 수 있다는 조건때문에 Set을 선택하셨군요. 👍
문자의 범위가 명확하게 정의되어 있다면 (e.g ASCII) 배열을 사용할 수도 있을 것 같아요!

아래 문제도 추가적으로 풀어보시면 sliding window 패턴을 익히시는데 도움이 될 것 같습니다
https://leetcode.com/problems/substring-with-concatenation-of-all-words

Copy link
Member Author

@dusunax dusunax Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ASCII코드 배열로 문자의 마지막 위치를 확인하는 방법이 있군요! 감사합니다

char_index = [-1] * 128 # SC: O(128)
for end in range(len(s)): # TC: O(n)
if char_index[ord(s[end])] >= start:
start = char_index[ord(s[end])] + 1
char_index[ord(s[end])] = end
max_count = max(max_count, end - start + 1)

추천해 주신 문제도 풀어보고 통과하면 댓글 달겠습니다😀

Copy link
Member Author

@dusunax dusunax Jan 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ekgns33
오래걸렸지만 추천해주신 문제 풀이해봤습니다🥹
permutation 경우를 변수에 저장했다가는 SC가 n!이 되는 점이 인상 깊었어요
슬라이딩 윈도우로 탐색할 때 어떤 데이터를 추적하는지 관련된 문제를 풀면서 예시를 많이 보면 좋겠다고 생각했습니다.

30. Substring with Concatenation of All Words


for end in range(len(s)): # TC: O(n)
while s[end] in substrings: # TC: Amortized O(1)
substrings.remove(s[start])
start += 1
substrings.add(s[end])
max_count = max(max_count, end - start + 1)

return max_count

def lengthOfLongestSubstring(self, s: str) -> int:
max_count = 0
start = 0
char_index = [-1] * 128 # SC: O(128)

for end in range(len(s)): # TC: O(n)
if char_index[ord(s[end])] >= start:
start = char_index[ord(s[end])] + 1
char_index[ord(s[end])] = end
max_count = max(max_count, end - start + 1)

return max_count
38 changes: 38 additions & 0 deletions number-of-islands/dusunax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'''
# 200. Number of Islands

use DFS to find the number of islands (to find the all the possible cases)

## Time and Space Complexity

```
TC: O(m * n)
SC: O(m * n)
```

### TC is O(m * n):
- dfs function is called for each cell in the grid for checking is the land ("1") = O(m * n)

### SC is O(m * n):
- 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)
'''
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
def dfs(x, y):
if x < 0 or y < 0 or y >= len(grid) or x >= len(grid[0]) or grid[y][x] == "0" :
return

grid[y][x] = "0"
dfs(x, y + 1)
dfs(x - 1, y)
dfs(x, y - 1)
dfs(x + 1, y)

island_count = 0
for y in range(len(grid)):
for x in range(len(grid[0])):
if grid[y][x] == "1":
dfs(x, y)
island_count += 1

return island_count
24 changes: 24 additions & 0 deletions reverse-linked-list/dusunax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'''
# 206. Reverse Linked List

iterate through the linked list and reverse the direction of the pointers.

## Time and Space Complexity

```
TC: O(n)
SC: O(1)
```
'''
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev = None
current = head

while current is not None: # TC: O(n)
next_list_temp = current.next
current.next = prev
prev = current
current = next_list_temp

return prev
67 changes: 67 additions & 0 deletions set-matrix-zeroes/dusunax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'''
# 73. Set Matrix Zeroes
# solution reference: https://www.algodale.com/problems/set-matrix-zeroes/
'''
class Solution:
'''
### TC is O(m * n):
- iterating through every cells, to find the zero cells. = O(m * n) 1️⃣
- iterating through every cells, to update the rows. = O(m * n) 2️⃣
- iterating through every cells, to update the columns. = O(m * n) 3️⃣

### SC is O(m + n):
- using each set to store the rows(O(m)) and columns(O(n)) that have zero. = O(m + n)
'''
def setZeroesWithSet(self, matrix: List[List[int]]) -> None:
zero_rows = set() # SC: O(m)
zero_cols = set() # SC: O(n)

for r in range(len(matrix)): # TC: O(m * n)
for c in range(len(matrix[0])):
if matrix[r][c] == 0:
zero_rows.add(r)
zero_cols.add(c)

for r in zero_rows: # TC: O(m * n)
for i in range(len(matrix[0])):
matrix[r][i] = 0

for c in zero_cols: # TC: O(m * n)
for i in range(len(matrix)):
matrix[i][c] = 0

'''
### TC is O(m * n):
- check if the first row or column has zero. = O(m + n)
- iterating through every cells, if it has zero, mark the first row and column. = O(m * n) 1️⃣
- update the matrix based on the marks(0) in the first row and column. = O(m * n) 2️⃣
- if the first row or column has zero, iterating through every cells, in the first row or column and updating it. = O(m + n)

### SC is O(1):
- using the first_row_has_zero and first_col_has_zero to store the zero information. = O(1)
'''
def setZeroesWithMarkerAndVariable(self, matrix: List[List[int]]) -> None:
rows = len(matrix)
cols = len(matrix[0])

first_row_has_zero = any(matrix[0][j] == 0 for j in range(cols)) # TC: O(n), SC: O(1)
first_col_has_zero = any(matrix[i][0] == 0 for i in range(rows)) # TC: O(m), SC: O(1)

for r in range(1, rows): # TC: O(m * n)
for c in range(1, cols):
if matrix[r][c] == 0:
matrix[r][0] = 0
matrix[0][c] = 0

for r in range(1, rows): # TC: O(m * n)
for c in range(1, cols):
if matrix[r][0] == 0 or matrix[0][c] == 0:
matrix[r][c] = 0

if first_row_has_zero:
for c in range(cols): # TC: O(n)
matrix[0][c] = 0

if first_col_has_zero:
for r in range(rows): # TC: O(m)
matrix[r][0] = 0
21 changes: 21 additions & 0 deletions unique-paths/dusunax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'''
# 62. Unique Paths

use dynamic programming & a dp table to store the number of ways to reach each cell.

### TC is O(m * n):
- iterating through every cell in the grid. = O(m * n)
- updating each cell in the grid. = O(1)

### SC is O(m * n):
- using a dp table (2D array) to store the number of ways to reach each cell. = O(m * n)
'''
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
dp = [[1] * n for _ in range(m)]

for y in range(1, m):
for x in range(1, n):
dp[y][x] = dp[y - 1][x] + dp[y][x - 1]

return dp[m - 1][n - 1]
Loading