Skip to content

Commit 6727b0b

Browse files
authored
Merge pull request #2256 from 8804who/main
[8804who] WEEK 09 Solutions
2 parents be3164b + e75576f commit 6727b0b

5 files changed

Lines changed: 137 additions & 0 deletions

File tree

linked-list-cycle/8804who.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def hasCycle(self, head: Optional[ListNode]) -> bool:
9+
fast = head
10+
slow = head
11+
while fast and fast.next:
12+
fast = fast.next.next
13+
slow = slow.next
14+
15+
if fast == slow:
16+
return True
17+
18+
return False
19+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def maxProduct(self, nums: List[int]) -> int:
3+
answer = max(nums)
4+
5+
cumprod = 1
6+
cumprod_to_first_neg = 1
7+
8+
for num in nums:
9+
if num == 0:
10+
cumprod = 1
11+
cumprod_to_first_neg = 1
12+
else:
13+
cumprod *= num
14+
if cumprod > 0:
15+
answer = max(answer, cumprod)
16+
else:
17+
answer = max(answer, cumprod//cumprod_to_first_neg)
18+
if cumprod_to_first_neg > 0:
19+
cumprod_to_first_neg *= num
20+
return answer
21+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from collections import Counter, defaultdict
2+
class Solution:
3+
def minWindow(self, s: str, t: str) -> str:
4+
answer = ''
5+
counter = Counter(t)
6+
now = defaultdict()
7+
8+
start = 0
9+
end = 0
10+
11+
now[s[start]] = 1
12+
13+
while start<=end and end < len(s):
14+
enough = True
15+
for key in counter.keys():
16+
if key not in now or now[key] < counter[key]:
17+
enough = False
18+
19+
if enough:
20+
if answer == '' or len(answer) > end-start+1:
21+
answer = s[start:end+1]
22+
23+
now[s[start]] -= 1
24+
start += 1
25+
else:
26+
end += 1
27+
if end == len(s):
28+
break
29+
if s[end] not in now:
30+
now[s[end]] = 0
31+
now[s[end]] += 1
32+
33+
return answer
34+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from collections import deque
2+
3+
class Solution:
4+
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
5+
answer = []
6+
h, w = len(heights), len(heights[0])
7+
moves = [[-1, 0], [1, 0], [0, -1], [0, 1]]
8+
9+
visited1 = [[False for _ in range(w)] for _ in range(h)]
10+
visited2 = [[False for _ in range(w)] for _ in range(h)]
11+
12+
q1 = deque()
13+
q2 = deque()
14+
15+
for i in range(w):
16+
q1.append((0, i))
17+
q2.append((h-1, i))
18+
visited1[0][i] = True
19+
visited2[h-1][i] = True
20+
21+
for i in range(h):
22+
q1.append((i, 0))
23+
q2.append((i, w-1))
24+
visited1[i][0] = True
25+
visited2[i][w-1] = True
26+
27+
while q1:
28+
y, x = q1.popleft()
29+
30+
for move in moves:
31+
next_y, next_x = y+move[0], x+move[1]
32+
33+
if 0 <= next_y < h and 0 <= next_x < w:
34+
if visited1[next_y][next_x]:
35+
continue
36+
if heights[next_y][next_x] >= heights[y][x]:
37+
visited1[next_y][next_x] = True
38+
q1.append((next_y, next_x))
39+
40+
while q2:
41+
y, x = q2.popleft()
42+
43+
for move in moves:
44+
next_y, next_x = y+move[0], x+move[1]
45+
46+
if 0 <= next_y < h and 0 <= next_x < w:
47+
if visited2[next_y][next_x]:
48+
continue
49+
if heights[next_y][next_x] >= heights[y][x]:
50+
visited2[next_y][next_x] = True
51+
q2.append((next_y, next_x))
52+
53+
for i in range(h):
54+
for j in range(w):
55+
if visited1[i][j] and visited2[i][j]:
56+
answer.append([i, j])
57+
return answer
58+

sum-of-two-integers/8804who.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import math
2+
class Solution:
3+
def getSum(self, a: int, b: int) -> int:
4+
return int(math.log(2**a * 2**b, 2))
5+

0 commit comments

Comments
 (0)