diff --git a/linked-list-cycle/8804who.py b/linked-list-cycle/8804who.py new file mode 100644 index 0000000000..57a9f797da --- /dev/null +++ b/linked-list-cycle/8804who.py @@ -0,0 +1,19 @@ +# 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: + fast = head + slow = head + while fast and fast.next: + fast = fast.next.next + slow = slow.next + + if fast == slow: + return True + + return False + diff --git a/maximum-product-subarray/8804who.py b/maximum-product-subarray/8804who.py new file mode 100644 index 0000000000..6eb3ca9fb5 --- /dev/null +++ b/maximum-product-subarray/8804who.py @@ -0,0 +1,21 @@ +class Solution: + def maxProduct(self, nums: List[int]) -> int: + answer = max(nums) + + cumprod = 1 + cumprod_to_first_neg = 1 + + for num in nums: + if num == 0: + cumprod = 1 + cumprod_to_first_neg = 1 + else: + cumprod *= num + if cumprod > 0: + answer = max(answer, cumprod) + else: + answer = max(answer, cumprod//cumprod_to_first_neg) + if cumprod_to_first_neg > 0: + cumprod_to_first_neg *= num + return answer + diff --git a/minimum-window-substring/8804who.py b/minimum-window-substring/8804who.py new file mode 100644 index 0000000000..c78faf9294 --- /dev/null +++ b/minimum-window-substring/8804who.py @@ -0,0 +1,34 @@ +from collections import Counter, defaultdict +class Solution: + def minWindow(self, s: str, t: str) -> str: + answer = '' + counter = Counter(t) + now = defaultdict() + + start = 0 + end = 0 + + now[s[start]] = 1 + + while start<=end and end < len(s): + enough = True + for key in counter.keys(): + if key not in now or now[key] < counter[key]: + enough = False + + if enough: + if answer == '' or len(answer) > end-start+1: + answer = s[start:end+1] + + now[s[start]] -= 1 + start += 1 + else: + end += 1 + if end == len(s): + break + if s[end] not in now: + now[s[end]] = 0 + now[s[end]] += 1 + + return answer + diff --git a/pacific-atlantic-water-flow/8804who.py b/pacific-atlantic-water-flow/8804who.py new file mode 100644 index 0000000000..ab1db81bb8 --- /dev/null +++ b/pacific-atlantic-water-flow/8804who.py @@ -0,0 +1,58 @@ +from collections import deque + +class Solution: + def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]: + answer = [] + h, w = len(heights), len(heights[0]) + moves = [[-1, 0], [1, 0], [0, -1], [0, 1]] + + visited1 = [[False for _ in range(w)] for _ in range(h)] + visited2 = [[False for _ in range(w)] for _ in range(h)] + + q1 = deque() + q2 = deque() + + for i in range(w): + q1.append((0, i)) + q2.append((h-1, i)) + visited1[0][i] = True + visited2[h-1][i] = True + + for i in range(h): + q1.append((i, 0)) + q2.append((i, w-1)) + visited1[i][0] = True + visited2[i][w-1] = True + + while q1: + y, x = q1.popleft() + + for move in moves: + next_y, next_x = y+move[0], x+move[1] + + if 0 <= next_y < h and 0 <= next_x < w: + if visited1[next_y][next_x]: + continue + if heights[next_y][next_x] >= heights[y][x]: + visited1[next_y][next_x] = True + q1.append((next_y, next_x)) + + while q2: + y, x = q2.popleft() + + for move in moves: + next_y, next_x = y+move[0], x+move[1] + + if 0 <= next_y < h and 0 <= next_x < w: + if visited2[next_y][next_x]: + continue + if heights[next_y][next_x] >= heights[y][x]: + visited2[next_y][next_x] = True + q2.append((next_y, next_x)) + + for i in range(h): + for j in range(w): + if visited1[i][j] and visited2[i][j]: + answer.append([i, j]) + return answer + diff --git a/sum-of-two-integers/8804who.py b/sum-of-two-integers/8804who.py new file mode 100644 index 0000000000..2104f5fbcb --- /dev/null +++ b/sum-of-two-integers/8804who.py @@ -0,0 +1,5 @@ +import math +class Solution: + def getSum(self, a: int, b: int) -> int: + return int(math.log(2**a * 2**b, 2)) +