From 78dae2dbb9530982854edaa3d990fcd1adf2f20d Mon Sep 17 00:00:00 2001 From: siroo Date: Sun, 19 Jan 2025 18:16:36 +0900 Subject: [PATCH 1/5] reverse linked list solution --- reverse-linked-list/jungsiroo.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 reverse-linked-list/jungsiroo.py diff --git a/reverse-linked-list/jungsiroo.py b/reverse-linked-list/jungsiroo.py new file mode 100644 index 000000000..42e75e488 --- /dev/null +++ b/reverse-linked-list/jungsiroo.py @@ -0,0 +1,29 @@ +class Solution: + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + # iterative way + + """ + prev, curr = None, head + while curr: + tmp_nxt = curr.next + + curr.next = prev + prev, curr = curr, tmp_nxt + + return prev + """ + + # Recursive Way + if head is None or head.next is None: + return head + + new_head = self.reverseList(head.next) + head.next.next = head # reversing pointer + head.next = None + return new_head + + # 둘 다 시간복잡도 O(n) + # 하지만 재귀의 경우 콜스택에 따른 공간복잡도 O(n)을 소요 + # iterative 방식은 O(1) + + From 5b0fcd39c7732081f4a567857e975028ddb48b98 Mon Sep 17 00:00:00 2001 From: siroo Date: Sun, 19 Jan 2025 18:29:49 +0900 Subject: [PATCH 2/5] longest substring w/o repeating chars solution --- .../jungsiroo.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 longest-substring-without-repeating-characters/jungsiroo.py diff --git a/longest-substring-without-repeating-characters/jungsiroo.py b/longest-substring-without-repeating-characters/jungsiroo.py new file mode 100644 index 000000000..3abfe579b --- /dev/null +++ b/longest-substring-without-repeating-characters/jungsiroo.py @@ -0,0 +1,31 @@ +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + """ + 딕셔너리를 사용하여 이전에 나왔던 인덱스를 기억 + 중복되지 않았다면 answer를 저장해 나감 + 중복이 발생하고 begin과 end사이라면 중복 발생 원인 다음을 begin으로 세팅 후 다시 계산 + + Time Complexity : O(n) + Space Complexity : O(n) + + """ + answer, begin, end = 0, 0, 0 + hash_map = dict() + + while end < len(s): + ch = s[end] + hash_map[ch] = hash_map.get(ch, -1) + + if hash_map[ch] == -1: #한 번도 나오지 않았다. + hash_map[ch] = end + answer = max(answer, end-begin+1) + else: + if begin<=hash_map[ch] Date: Sun, 19 Jan 2025 19:18:00 +0900 Subject: [PATCH 3/5] number of islands solution --- number-of-islands/jungsiroo.py | 50 ++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 number-of-islands/jungsiroo.py diff --git a/number-of-islands/jungsiroo.py b/number-of-islands/jungsiroo.py new file mode 100644 index 000000000..1c34a3f2a --- /dev/null +++ b/number-of-islands/jungsiroo.py @@ -0,0 +1,50 @@ +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + # TC : O(n*m) + # SC : O(n*m) + + m, n = len(grid), len(grid[0]) + dx = [-1,1,0,0] + dy = [0,0,-1,1] + + def in_range(r, c): + if r<0 or c<0 or r>=m or c>=n: + return False + return True + + # DFS Way + + def dfs(r, c): + grid[r][c] = 'x' + + for i in range(4): + nr, nc = r+dx[i], c+dy[i] + if not in_range(nr, nc) or grid[nr][nc] != '1': + continue + dfs(nr, nc) + + # BFS Way + from collections import deque + def bfs(r, c): + grid[r][c] = 'x' + queue = deque() + queue.append([r, c]) + + while queue: + cr, cc = queue.popleft() + for i in range(4): + nr, nc = cr+dx[i], cc+dy[i] + + if not in_range(nr, nc) or grid[nr][nc] != '1': + continue + grid[nr][nc] = 'x' + queue.append([nr, nc]) + + ret = 0 + for i in range(m): + for j in range(n): + if grid[i][j] == '1': + bfs(i, j) + ret += 1 + return ret + From 908440e191747e8d005c76ccd831df9fd36b81a2 Mon Sep 17 00:00:00 2001 From: siroo Date: Sun, 19 Jan 2025 19:36:24 +0900 Subject: [PATCH 4/5] unique paths --- unique-paths/jungsiroo.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 unique-paths/jungsiroo.py diff --git a/unique-paths/jungsiroo.py b/unique-paths/jungsiroo.py new file mode 100644 index 000000000..ac92d1638 --- /dev/null +++ b/unique-paths/jungsiroo.py @@ -0,0 +1,15 @@ +from math import comb + +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + # mathematic way + # 중학교 때 배운 최단거리 조합으로 구하기 문제 + # 문제 자체가 오른쪽, 아래밖에 못가기에 최단거리가 될 수 밖에 없음 + + """ + TC : O(max(m, n)) + SC : O(1) + """ + + return comb(m+n-2, m-1) + From 39f82b31fdde07af18d3a04be881c4f84ad2b095 Mon Sep 17 00:00:00 2001 From: siroo Date: Sun, 19 Jan 2025 19:52:19 +0900 Subject: [PATCH 5/5] set matrix zeroes --- set-matrix-zeroes/jungsiroo.py | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 set-matrix-zeroes/jungsiroo.py diff --git a/set-matrix-zeroes/jungsiroo.py b/set-matrix-zeroes/jungsiroo.py new file mode 100644 index 000000000..3e33e4508 --- /dev/null +++ b/set-matrix-zeroes/jungsiroo.py @@ -0,0 +1,43 @@ +class Solution: + def setZeroes(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + + """ + # Naive change : save rows and cols + # SC : O(m+n) + row_zero, col_zero = set(), set() + + rows, cols = len(matrix), len(matrix[0]) + for i in range(rows): + for j in range(cols): + if matrix[i][j] == 0: + row_zero.add(i) + col_zero.add(j) + + for i in range(rows): + for j in range(cols): + if i in row_zero or j in col_zero: + matrix[i][j] = 0 + """ + + # Constant Space Complexity using bitmasking + # 0인 구간을 toggle 시켜놓고 확인하는 방법 + def is_on(number, k): + return (number & (1<