diff --git a/3sum/gcount85.py b/3sum/jamiebase.py similarity index 100% rename from 3sum/gcount85.py rename to 3sum/jamiebase.py diff --git a/best-time-to-buy-and-sell-stock/gcount85.py b/best-time-to-buy-and-sell-stock/jamiebase.py similarity index 100% rename from best-time-to-buy-and-sell-stock/gcount85.py rename to best-time-to-buy-and-sell-stock/jamiebase.py diff --git a/climbing-stairs/gcount85.py b/climbing-stairs/jamiebase.py similarity index 100% rename from climbing-stairs/gcount85.py rename to climbing-stairs/jamiebase.py diff --git a/coin-change/gcount85.py b/coin-change/jamiebase.py similarity index 100% rename from coin-change/gcount85.py rename to coin-change/jamiebase.py diff --git a/combination-sum/gcount85.py b/combination-sum/jamiebase.py similarity index 100% rename from combination-sum/gcount85.py rename to combination-sum/jamiebase.py diff --git a/container-with-most-water/gcount85.py b/container-with-most-water/jamiebase.py similarity index 100% rename from container-with-most-water/gcount85.py rename to container-with-most-water/jamiebase.py diff --git a/contains-duplicate/gcount85.py b/contains-duplicate/jamiebase.py similarity index 100% rename from contains-duplicate/gcount85.py rename to contains-duplicate/jamiebase.py diff --git a/decode-ways/gcount85.py b/decode-ways/jamiebase.py similarity index 100% rename from decode-ways/gcount85.py rename to decode-ways/jamiebase.py diff --git a/find-minimum-in-rotated-sorted-array/gcount85.py b/find-minimum-in-rotated-sorted-array/jamiebase.py similarity index 100% rename from find-minimum-in-rotated-sorted-array/gcount85.py rename to find-minimum-in-rotated-sorted-array/jamiebase.py diff --git a/group-anagrams/gcount85.py b/group-anagrams/jamiebase.py similarity index 100% rename from group-anagrams/gcount85.py rename to group-anagrams/jamiebase.py diff --git a/house-robber/gcount85.py b/house-robber/jamiebase.py similarity index 100% rename from house-robber/gcount85.py rename to house-robber/jamiebase.py diff --git a/implement-trie-prefix-tree/gcount85.py b/implement-trie-prefix-tree/jamiebase.py similarity index 100% rename from implement-trie-prefix-tree/gcount85.py rename to implement-trie-prefix-tree/jamiebase.py diff --git a/longest-consecutive-sequence/gcount85.py b/longest-consecutive-sequence/jamiebase.py similarity index 100% rename from longest-consecutive-sequence/gcount85.py rename to longest-consecutive-sequence/jamiebase.py diff --git a/longest-increasing-subsequence/gcount85.py b/longest-increasing-subsequence/jamiebase.py similarity index 100% rename from longest-increasing-subsequence/gcount85.py rename to longest-increasing-subsequence/jamiebase.py diff --git a/longest-substring-without-repeating-characters/jamiebase.py b/longest-substring-without-repeating-characters/jamiebase.py new file mode 100644 index 0000000000..2701a819d3 --- /dev/null +++ b/longest-substring-without-repeating-characters/jamiebase.py @@ -0,0 +1,33 @@ +""" +# Approach +슬라이딩 윈도우를 사용한다. +right 포인터를 하나씩 이동시키면서 문자열을 확장하고, +이미 등장한 문자를 만나면 해당 문자의 마지막 위치를 기준으로 left 포인터를 이동시켜 중복이 없는 구간을 유지한다. +각 단계에서 현재 윈도우의 길이를 계산하여 최대값을 갱신한다. + +# Complexity +문자열 길이를 N이라고 할 때, 문자 종류 수를 K라고 할 때 +- Time complexity: O(N) +- Space complexity: O(K) +""" + + +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + last_seen = {} # 문자 -> 마지막으로 등장한 인덱스 + left = 0 # 현재 윈도우의 시작 위치 + answer = 0 # 최대 길이 저장 + + for right, ch in enumerate(s): + # 현재 문자가 이전에 등장했고, + # 그 위치가 현재 윈도우 안에 있다면 left 이동 + if ch in last_seen and last_seen[ch] >= left: + left = last_seen[ch] + 1 + + # 현재 문자의 마지막 위치 갱신 + last_seen[ch] = right + + # 현재 윈도우 길이 계산 및 최대값 갱신 + answer = max(answer, right - left + 1) + + return answer diff --git a/maximum-depth-of-binary-tree/gcount85.py b/maximum-depth-of-binary-tree/jamiebase.py similarity index 100% rename from maximum-depth-of-binary-tree/gcount85.py rename to maximum-depth-of-binary-tree/jamiebase.py diff --git a/maximum-subarray/gcount85.py b/maximum-subarray/jamiebase.py similarity index 100% rename from maximum-subarray/gcount85.py rename to maximum-subarray/jamiebase.py diff --git a/merge-two-sorted-lists/gcount85.py b/merge-two-sorted-lists/jamiebase.py similarity index 100% rename from merge-two-sorted-lists/gcount85.py rename to merge-two-sorted-lists/jamiebase.py diff --git a/number-of-1-bits/gcount85.py b/number-of-1-bits/jamiebase.py similarity index 100% rename from number-of-1-bits/gcount85.py rename to number-of-1-bits/jamiebase.py diff --git a/number-of-islands/jamiebase.py b/number-of-islands/jamiebase.py new file mode 100644 index 0000000000..83a90ca814 --- /dev/null +++ b/number-of-islands/jamiebase.py @@ -0,0 +1,45 @@ +""" +# Approach +Grid의 원소에 일일이 dfs를 수행하되 값이 "1"(=육지)인 경우만 탐색을 수행합니다. +이때 방문한 노드는 값을 "-1"로 변경하여 방문 체크합니다. + +# Complexity +Grid의 rows = M, cols = N이라고 할 때, +- Time complexity: O(M*N) +- Space complexity: visited set 최대 O(M*N) +""" + + +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + answer = 0 + direction = [(0, 1), (0, -1), (1, 0), (-1, 0)] + row = len(grid) + col = len(grid[0]) + + def can_go(x, y, row, col): + if x < 0 or y < 0 or x >= row or y >= col: + return False + if grid[x][y] == "0": + return False + return True + + def dfs(x, y, row, col): + for dx, dy in direction: + nx, ny = x + dx, y + dy + if not can_go(nx, ny, row, col): + continue + if grid[nx][ny] == "-1": # 방문 확인 + continue + grid[nx][ny] = "-1" # 방문 처리 + dfs(nx, ny, row, col) + + for x, row_list in enumerate(grid): + for y, value in enumerate(row_list): + if value != "1": + continue + grid[x][y] = "-1" # 방문 확인 + dfs(x, y, row, col) + answer += 1 + + return answer diff --git a/product-of-array-except-self/gcount85.py b/product-of-array-except-self/jamiebase.py similarity index 100% rename from product-of-array-except-self/gcount85.py rename to product-of-array-except-self/jamiebase.py diff --git a/reverse-linked-list/jamiebase.py b/reverse-linked-list/jamiebase.py new file mode 100644 index 0000000000..4059965740 --- /dev/null +++ b/reverse-linked-list/jamiebase.py @@ -0,0 +1,22 @@ +""" +# Approach +이전 노드, 현재 노드, 다음 노드를 저장한 뒤, 현재 노드가 이전 노드를 가리키도록 방향을 바꾸고, +이전 노드와 현재 노드 포인터를 이동시킵니다. + +# Complexity +List의 길이를 N이라고 할 떄 +- Time complexity: O(N) +- Space complexity: O(1) +""" + + +class Solution: + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + prev = None + cur = head + while cur: + nxt = cur.next # 1. 다음 노드 저장 + cur.next = prev # 2. 방향 뒤집기 + prev = cur # 3. prev 이동 + cur = nxt # 4. cur 이동 + return prev diff --git a/set-matrix-zeroes/jamiebase.py b/set-matrix-zeroes/jamiebase.py new file mode 100644 index 0000000000..f9f51d4c9d --- /dev/null +++ b/set-matrix-zeroes/jamiebase.py @@ -0,0 +1,34 @@ +""" +# Approach +matrix를 순회하며 값이 0인 행과 열의 번호를 기록합니다. +기록한 것을 토대로 다시 matrix를 순회하며 0으로 채웁니다. + +# Complexity +matrix의 행 크기를 M, 열 크기를 N이라고 할 때 +- Time complexity: O(MN) +- Space complexity: O(M+N) +""" + + +class Solution: + def setZeroes(self, matrix: list[list[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + + rows, cols = len(matrix), len(matrix[0]) + zero_rows = set() + zero_cols = set() + for x, row in enumerate(matrix): + for y, value in enumerate(row): + if value == 0: + zero_rows.add(x) + zero_cols.add(y) + + for x in zero_rows: + for i in range(cols): + matrix[x][i] = 0 + + for y in zero_cols: + for i in range(rows): + matrix[i][y] = 0 diff --git a/spiral-matrix/gcount85.py b/spiral-matrix/jamiebase.py similarity index 100% rename from spiral-matrix/gcount85.py rename to spiral-matrix/jamiebase.py diff --git a/top-k-frequent-elements/gcount85.py b/top-k-frequent-elements/jamiebase.py similarity index 100% rename from top-k-frequent-elements/gcount85.py rename to top-k-frequent-elements/jamiebase.py diff --git a/two-sum/gcount85.py b/two-sum/jamiebase.py similarity index 100% rename from two-sum/gcount85.py rename to two-sum/jamiebase.py diff --git a/unique-paths/jamiebase.py b/unique-paths/jamiebase.py new file mode 100644 index 0000000000..feee38c30e --- /dev/null +++ b/unique-paths/jamiebase.py @@ -0,0 +1,16 @@ +""" +# Approach +(0,0) -> (m-1, n-1)로 가기 위해서는 아래로는 m-1회, 우측으로 n-1회 이동해야 합니다. +도합 m+n-2회 이동해야 합니다. 그 이동 경로 중에서 방향 전환(우측 이동 혹은 아래쪽 이동)을 하는 경우의 수를 계산하여 unique path를 구합니다. + +# Complexity +- Time complexity: math.comb는 거의 상수 시간으로 취급하므로 O(1) +- Space complexity: O(1) +""" + +import math + + +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + return math.comb(m + n - 2, m - 1) diff --git a/valid-anagram/gcount85.py b/valid-anagram/jamiebase.py similarity index 100% rename from valid-anagram/gcount85.py rename to valid-anagram/jamiebase.py diff --git a/valid-palindrome/gcount85.py b/valid-palindrome/jamiebase.py similarity index 100% rename from valid-palindrome/gcount85.py rename to valid-palindrome/jamiebase.py diff --git a/valid-parentheses/gcount85.py b/valid-parentheses/jamiebase.py similarity index 100% rename from valid-parentheses/gcount85.py rename to valid-parentheses/jamiebase.py diff --git a/validate-binary-search-tree/gcount85.py b/validate-binary-search-tree/jamiebase.py similarity index 100% rename from validate-binary-search-tree/gcount85.py rename to validate-binary-search-tree/jamiebase.py diff --git a/word-break/gcount85.py b/word-break/jamiebase.py similarity index 100% rename from word-break/gcount85.py rename to word-break/jamiebase.py diff --git a/word-search/gcount85.py b/word-search/jamiebase.py similarity index 100% rename from word-search/gcount85.py rename to word-search/jamiebase.py