diff --git a/linked-list-cycle/yuseok89.py b/linked-list-cycle/yuseok89.py new file mode 100644 index 0000000000..6cb4c47072 --- /dev/null +++ b/linked-list-cycle/yuseok89.py @@ -0,0 +1,22 @@ +# TC: O(N) +# SC: O(1) +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def hasCycle(self, head: Optional[ListNode]) -> bool: + if not head: + return False + + slow, fast = head, head.next + while slow is not fast: + if not fast or not fast.next: + return False + + slow, fast = slow.next, fast.next.next + + return True + diff --git a/maximum-product-subarray/yuseok89.py b/maximum-product-subarray/yuseok89.py new file mode 100644 index 0000000000..bfb3cbb99f --- /dev/null +++ b/maximum-product-subarray/yuseok89.py @@ -0,0 +1,17 @@ +# TC: O(N) +# SC: O(1) +class Solution: + def maxProduct(self, nums: List[int]) -> int: + + ans = nums[0] + max_val, min_val = 1, 1 + + for num in nums: + temp = max_val * num + max_val = max(temp, min_val * num, num) + min_val = min(temp, min_val * num, num) + + ans = max(ans, max_val) + + return ans + diff --git a/minimum-window-substring/yuseok89.py b/minimum-window-substring/yuseok89.py new file mode 100644 index 0000000000..728a516eb9 --- /dev/null +++ b/minimum-window-substring/yuseok89.py @@ -0,0 +1,45 @@ +# TC: O(N) +# SC: O(N) +class Solution: + def minWindow(self, s: str, t: str) -> str: + + len_s = len(s) + len_t = len(t) + + min_len = len_s + 1 + ans_start_idx = -1 + total_cnt = 0 + + t_cnt_dict = Counter(t) + cnt_dict = defaultdict(int) + + l, r = 0, 0 + + while r < len_s: + + r_c = s[r] + + if cnt_dict[r_c] < t_cnt_dict.get(r_c, 0): + total_cnt += 1 + + cnt_dict[r_c] += 1 + r += 1 + + while total_cnt == len_t: + if min_len > r - l: + min_len = r - l + ans_start_idx = l + + l_c = s[l] + + if cnt_dict[l_c] <= t_cnt_dict[l_c]: + total_cnt -= 1 + + cnt_dict[l_c] -= 1 + l += 1 + + if ans_start_idx == -1: + return '' + + return s[ans_start_idx:ans_start_idx + min_len] + diff --git a/pacific-atlantic-water-flow/yuseok89.py b/pacific-atlantic-water-flow/yuseok89.py new file mode 100644 index 0000000000..598901b20b --- /dev/null +++ b/pacific-atlantic-water-flow/yuseok89.py @@ -0,0 +1,44 @@ +# TC: O(N*M) +# SC: O(N*M) +class Solution: + def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]: + + n = len(heights) + m = len(heights[0]) + + dirs = [[0, 1], [0, -1], [1, 0], [-1, 0]] + + def dfs(row, col, check): + check[row][col] = True + + 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 not check[new_row][new_col] and heights[row][col] <= heights[new_row][new_col]: + dfs(new_row, new_col, check) + + pac_check = [[False for _ in range(m)] for _ in range(n)] + atl_check = [[False for _ in range(m)] for _ in range(n)] + + for row in range(1, n): + dfs(row, 0, pac_check) + + for col in range(m): + dfs(0, col, pac_check) + + for row in range(n - 1): + dfs(row, m - 1, atl_check) + + for col in range(m): + dfs(n - 1, col, atl_check) + + ans = [] + + for row in range(n): + for col in range(m): + if pac_check[row][col] and atl_check[row][col]: + ans.append([row, col]) + + return ans + diff --git a/sum-of-two-integers/yuseok89.py b/sum-of-two-integers/yuseok89.py new file mode 100644 index 0000000000..9052b32c29 --- /dev/null +++ b/sum-of-two-integers/yuseok89.py @@ -0,0 +1,19 @@ +# TC: O(12) +# SC: O(1) +class Solution: + def getSum(self, a: int, b: int) -> int: + MASK = 0b111111111111 + + a = a & MASK + b = b & MASK + + while b != 0: + c = (a & b) << 1 + a = a ^ b + b = c & MASK + + if a > (MASK >> 1): + a = ~(a ^ MASK) + + return a +