diff --git a/longest-substring-without-repeating-characters/yuseok89.py b/longest-substring-without-repeating-characters/yuseok89.py new file mode 100644 index 0000000000..2c0aaafdd5 --- /dev/null +++ b/longest-substring-without-repeating-characters/yuseok89.py @@ -0,0 +1,19 @@ +# TC: O(N) +# SC: O(K) +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + idx_map = {} + start = 0 + ans = 0 + + for end in range(len(s)): + c = s[end] + if c in idx_map and start <= idx_map[c]: + start = idx_map[c] + 1 + else: + ans = max(ans, end - start + 1) + + idx_map[c] = end + + return ans + diff --git a/number-of-islands/yuseok89.py b/number-of-islands/yuseok89.py new file mode 100644 index 0000000000..d30efbc2e4 --- /dev/null +++ b/number-of-islands/yuseok89.py @@ -0,0 +1,28 @@ +# TC: O(NM) +# SC: O(NM) +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + ans = 0 + n = len(grid) + m = len(grid[0]) + + dirs = [[-1, 0], [1, 0], [0, -1], [0, 1]] + + def fill(row: int, col: int): + grid[row][col] = '0' + + for dir in dirs: + new_row = row + dir[0] + new_col = col + dir[1] + + if 0 <= new_row < n and 0 <= new_col < m and grid[new_row][new_col] == '1': + fill(new_row, new_col) + + for i in range(0, n): + for j in range(0, m): + if grid[i][j] == '1': + fill(i, j) + ans += 1 + + return ans + diff --git a/reverse-linked-list/yuseok89.py b/reverse-linked-list/yuseok89.py new file mode 100644 index 0000000000..36d7d96f8b --- /dev/null +++ b/reverse-linked-list/yuseok89.py @@ -0,0 +1,21 @@ +# TC: O(N) +# SC: O(1) +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + + prev = None + cur = head + + while cur: + nxt = cur.next + cur.next = prev + prev = cur + cur = nxt + + return prev + diff --git a/set-matrix-zeroes/yuseok89.py b/set-matrix-zeroes/yuseok89.py new file mode 100644 index 0000000000..08949bf4b6 --- /dev/null +++ b/set-matrix-zeroes/yuseok89.py @@ -0,0 +1,25 @@ +# TC: O(NM) +# SC: O(N+M) +class Solution: + def setZeroes(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + + n = len(matrix) + m = len(matrix[0]) + + row_set = set() + col_set = set() + + for row in range(0, n): + for col in range(0, m): + if matrix[row][col] == 0: + row_set.add(row) + col_set.add(col) + + for row in range(0, n): + for col in range(0, m): + if row in row_set or col in col_set: + matrix[row][col] = 0 + diff --git a/unique-paths/yuseok89.py b/unique-paths/yuseok89.py new file mode 100644 index 0000000000..74e162d177 --- /dev/null +++ b/unique-paths/yuseok89.py @@ -0,0 +1,14 @@ +# TC: O(NM) +# SC: O(N) +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + + cnt = [0] * n + cnt[0] = 1 + + for i in range(0, m): + for j in range(1, n): + cnt[j] += cnt[j - 1] + + return cnt[n - 1] +