From 330eded517f86fc80adb78c7d2bf9d922f71d2d0 Mon Sep 17 00:00:00 2001 From: kimseongeun Date: Mon, 12 May 2025 11:49:32 +0900 Subject: [PATCH 1/6] week 7 reverse-linked-list --- reverse-linked-list/i-mprovising.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 reverse-linked-list/i-mprovising.py diff --git a/reverse-linked-list/i-mprovising.py b/reverse-linked-list/i-mprovising.py new file mode 100644 index 000000000..d955fb268 --- /dev/null +++ b/reverse-linked-list/i-mprovising.py @@ -0,0 +1,25 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + """ + Time complexity O(n) + Space complexity O(1) + """ + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + if not head or not head.next: + return head + + node = head.next + prev = head + prev.next = None + + while node: + next_node = node.next + node.next = prev + prev = node + node = next_node + + return prev From b78caa75c263ef045a1e3da88573662881019b29 Mon Sep 17 00:00:00 2001 From: kimseongeun Date: Mon, 12 May 2025 15:27:02 +0900 Subject: [PATCH 2/6] longest-substring-without-repeating-characters --- .../i-mprovising.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 longest-substring-without-repeating-characters/i-mprovising.py diff --git a/longest-substring-without-repeating-characters/i-mprovising.py b/longest-substring-without-repeating-characters/i-mprovising.py new file mode 100644 index 000000000..0dcc7c2a1 --- /dev/null +++ b/longest-substring-without-repeating-characters/i-mprovising.py @@ -0,0 +1,38 @@ +from collections import deque + +class Solution: + """ + Time complexity O(n) + Space complexity O(n) + """ + def lengthOfLongestSubstring(self, s: str) -> int: + max_len = 0 + q = deque() + for i, ch in enumerate(s): + if ch not in q: + q.append(ch) + if len(q) > max_len: + max_len = len(q) + else: + while True: + tmp = q.popleft() + if tmp == ch: + break + q.append(ch) + + return max_len + + def slidingWindow(self, s): + start, end = 0, 0 + substr = set() + max_len = 0 + while end < len(s): + if s[end] in substr: + substr.remove(s[start]) + start += 1 + else: + substr.add(s[end]) + end += 1 + max_len = max(end - start, max_len) + return max_len + From 3113880d800bdf8d888421d939fc9dd0dd1faa79 Mon Sep 17 00:00:00 2001 From: kimseongeun Date: Mon, 12 May 2025 15:42:16 +0900 Subject: [PATCH 3/6] number-of-islands --- number-of-islands/i-mprovising.py | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 number-of-islands/i-mprovising.py diff --git a/number-of-islands/i-mprovising.py b/number-of-islands/i-mprovising.py new file mode 100644 index 000000000..789d0d8e2 --- /dev/null +++ b/number-of-islands/i-mprovising.py @@ -0,0 +1,32 @@ +class Solution: + """ + Time, Space comlexity O(n*m) + + connected components + dfs, bfs + """ + def numIslands(self, grid: List[List[str]]) -> int: + n, m = len(grid), len(grid[0]) + visited = [[False for _ in range(m)] for _ in range(n)] # visited 대신 grid를 0으로 표시할수도 있다 + islands = 0 + + def dfs(x, y): + stack = [(x, y)] + while stack: + x, y = stack.pop() + dx = [-1, 1, 0, 0] + dy = [0, 0, -1, 1] + for k in range(4): + nx, ny = x + dx[k], y + dy[k] + if 0<=nx<=n-1 and 0<=ny<=m-1: + if not visited[nx][ny] and grid[nx][ny] == "1": + visited[nx][ny] = True + stack.append((nx, ny)) + + for i in range(n): + for j in range(m): + if not visited[i][j] and grid[i][j] == "1": + dfs(i, j) + islands += 1 + + return islands \ No newline at end of file From 1ff99368129fbda05aa4426c3b5f7d973a05c673 Mon Sep 17 00:00:00 2001 From: kimseongeun Date: Mon, 12 May 2025 15:43:12 +0900 Subject: [PATCH 4/6] fix linelint --- longest-substring-without-repeating-characters/i-mprovising.py | 1 - number-of-islands/i-mprovising.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/longest-substring-without-repeating-characters/i-mprovising.py b/longest-substring-without-repeating-characters/i-mprovising.py index 0dcc7c2a1..a3340ec12 100644 --- a/longest-substring-without-repeating-characters/i-mprovising.py +++ b/longest-substring-without-repeating-characters/i-mprovising.py @@ -35,4 +35,3 @@ def slidingWindow(self, s): end += 1 max_len = max(end - start, max_len) return max_len - diff --git a/number-of-islands/i-mprovising.py b/number-of-islands/i-mprovising.py index 789d0d8e2..29de276f0 100644 --- a/number-of-islands/i-mprovising.py +++ b/number-of-islands/i-mprovising.py @@ -29,4 +29,4 @@ def dfs(x, y): dfs(i, j) islands += 1 - return islands \ No newline at end of file + return islands From e19e8c10b9170b6b686f62cc900f8da440bdaf6d Mon Sep 17 00:00:00 2001 From: kimseongeun Date: Thu, 15 May 2025 16:03:10 +0900 Subject: [PATCH 5/6] add unique-paths --- unique-paths/i-mprovising.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 unique-paths/i-mprovising.py diff --git a/unique-paths/i-mprovising.py b/unique-paths/i-mprovising.py new file mode 100644 index 000000000..1b8d9366f --- /dev/null +++ b/unique-paths/i-mprovising.py @@ -0,0 +1,21 @@ +""" +Time, space complexity O(m*n) + +Dynamic programming +""" + +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + dp = [[0 for _ in range(n)] for _ in range(m)] + dp[0] = [1 for _ in range(n)] + + for i in range(1, m): + for j in range(n): + if i == 0: + continue + if j == 0: + dp[i][j] = 1 + continue + dp[i][j] = dp[i-1][j] + dp[i][j-1] + + return dp[-1][-1] From 5a40ebe121dbcb395ccd118de8d48f5b4c588161 Mon Sep 17 00:00:00 2001 From: kimseongeun Date: Thu, 15 May 2025 16:14:53 +0900 Subject: [PATCH 6/6] add set-matrix-zeroes --- set-matrix-zeroes/i-mprovising.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 set-matrix-zeroes/i-mprovising.py diff --git a/set-matrix-zeroes/i-mprovising.py b/set-matrix-zeroes/i-mprovising.py new file mode 100644 index 000000000..3a9daf602 --- /dev/null +++ b/set-matrix-zeroes/i-mprovising.py @@ -0,0 +1,23 @@ +""" +Time, space complexity O(m * n) +""" + +class Solution: + def setZeroes(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + m, n = len(matrix), len(matrix[0]) + i_indices = set() + j_indices = set() + for i in range(m): + for j in range(n): + if matrix[i][j] == 0: + i_indices.add(i) + j_indices.add(j) + + for i in i_indices: + matrix[i] = [0 for _ in range(n)] + for j in j_indices: + for i in range(m): + matrix[i][j] = 0