diff --git a/longest-substring-without-repeating-characters/pmjuu.py b/longest-substring-without-repeating-characters/pmjuu.py new file mode 100644 index 000000000..c63d4c972 --- /dev/null +++ b/longest-substring-without-repeating-characters/pmjuu.py @@ -0,0 +1,23 @@ +''' +시간복잡도: O(n) +- right와 left 포인터가 각각 최대 n번 움직임 + +공간복잡도: O(n) +- hars 집합이 최대 n개의 고유 문자를 저장할 수 있 +''' + +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + length = 0 + left = 0 + chars = set() + + for right in range(len(s)): + if s[right] in chars: + while s[right] in chars: + chars.remove(s[left]) + left += 1 + chars.add(s[right]) + length = max(length, right + 1 - left) + + return length diff --git a/number-of-islands/pmjuu.py b/number-of-islands/pmjuu.py new file mode 100644 index 000000000..3e105ca3a --- /dev/null +++ b/number-of-islands/pmjuu.py @@ -0,0 +1,37 @@ +''' +시간 복잡도: O(m * n) +- 각 셀을 한 번씩 방문하며, DFS를 통해 연결된 모든 셀을 확인하므로 grid의 모든 셀에 대해 O(1) 작업이 수행됩니다. + +공간 복잡도: O(m * n) +- 최악의 경우, DFS의 호출 스택이 격자의 모든 셀에 대해 쌓일 수 있습니다. 이 경우 스택에 저장되는 셀의 개수는 최대 m * n 입니다. +''' + +from typing import List + + +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + count = 0 + m, n = len(grid), len(grid[0]) + + def exploreIsland(row: int, col: int): + withinBounds = (0 <= row < m) and (0 <= col < n) + if not withinBounds or grid[row][col] != "1": + return + + grid[row][col] = "#" + + exploreIsland(row + 1, col) + exploreIsland(row - 1, col) + exploreIsland(row, col + 1) + exploreIsland(row, col - 1) + + # Iterate through all cells in the grid + for i in range(m): + for j in range(n): + # if a cell is "1", increment count and mark the whole island as "#" + if grid[i][j] == "1": + count += 1 + exploreIsland(i, j) + + return count diff --git a/reverse-linked-list/pmjuu.py b/reverse-linked-list/pmjuu.py new file mode 100644 index 000000000..05283f3f4 --- /dev/null +++ b/reverse-linked-list/pmjuu.py @@ -0,0 +1,24 @@ +''' +시간 복잡도: O(n) +- 리스트의 모든 노드를 한 번씩 방문하므로 시간 복잡도는 O(n)입니다. + +공간 복잡도: O(n) +- 재귀 호출을 사용하므로 호출 스택에 최대 n번의 함수 호출이 쌓이므로 공간 복잡도는 O(n)입니다. +''' +from typing import Optional + +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]: + if not head or not head.next: + return head + + new_head = self.reverseList(head.next) # find the last node + head.next.next = head # reverse + head.next = None # remove cycle + + return new_head diff --git a/set-matrix-zeroes/pmjuu.py b/set-matrix-zeroes/pmjuu.py new file mode 100644 index 000000000..c63fb07f9 --- /dev/null +++ b/set-matrix-zeroes/pmjuu.py @@ -0,0 +1,48 @@ +''' +시간복잡도: O(m * n) +공간복잡도: O(1) +''' + +from typing import List + + +class Solution: + def setZeroes(self, matrix: List[List[int]]) -> None: + m, n = len(matrix), len(matrix[0]) + first_row_zero = False # Flag to check if the first row needs to be zeroed + first_col_zero = False # Flag to check if the first column needs to be zeroed + + # Check if the first row has any zeros + for j in range(n): + if matrix[0][j] == 0: + first_row_zero = True + break + + # Check if the first column has any zeros + for i in range(m): + if matrix[i][0] == 0: + first_col_zero = True + break + + # Use the first row and column to mark rows and columns that need to be zeroed + for i in range(1, m): + for j in range(1, n): + if matrix[i][j] == 0: + matrix[i][0] = 0 + matrix[0][j] = 0 + + # Zero out cells based on markers in the first row and column + for i in range(1, m): + for j in range(1, n): + if matrix[i][0] == 0 or matrix[0][j] == 0: + matrix[i][j] = 0 + + # Zero out the first row if needed + if first_row_zero: + for j in range(n): + matrix[0][j] = 0 + + # Zero out the first column if needed + if first_col_zero: + for i in range(m): + matrix[i][0] = 0 diff --git a/unique-paths/pmjuu.py b/unique-paths/pmjuu.py new file mode 100644 index 000000000..c384a8fd5 --- /dev/null +++ b/unique-paths/pmjuu.py @@ -0,0 +1,18 @@ +''' +시간 복잡도: O(m * n) +- 동적 프로그래밍 테이블(dp)을 사용하여 각 셀에서의 경로 수를 한 번씩 계산하므로 시간 복잡도는 격자의 모든 셀에 대해 O(m * n)입니다. + +공간 복잡도: O(m * n) +- dp 테이블을 사용하여 모든 셀에 대한 경로 수를 저장하므로 공간 복잡도는 O(m * n)입니다. +''' + +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + # save number of unique paths to each cell + dp = [[1] * n for _ in range(m)] + + for row in range(1, m): + for col in range(1, n): + dp[row][col] = dp[row][col - 1] + dp[row - 1][col] + + return dp[m - 1][n - 1]