Skip to content

Commit bf195e0

Browse files
authored
Merge pull request #1913 from devyejin/main
[devyejin] WEEK 09 solutions
2 parents 01edee7 + b34981c commit bf195e0

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

linked-list-cycle/devyejin.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Definition for singly-linked list.
2+
class ListNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.next = None
6+
7+
from typing import Optional
8+
9+
# time complexity O(n)
10+
# space complexity O(1)
11+
class Solution:
12+
def hasCycle(self, head: Optional[ListNode]) -> bool:
13+
slow, fast = head, head
14+
while fast and fast.next:
15+
slow = slow.next
16+
fast = fast.next.next
17+
18+
if slow == fast:
19+
return True
20+
return False
21+
22+
23+
# time complexity O(n)
24+
# space complexity O(n)
25+
# class Solution:
26+
# def hasCycle(self, head: Optional[ListNode]) -> bool:
27+
# visited = set()
28+
# while head:
29+
# if head in visited:
30+
# return True
31+
# visited.add(head)
32+
# head = head.next
33+
# return False
34+
35+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
# time O(n), space O(1)
4+
class Solution:
5+
def maxProduct(self, nums: List[int]) -> int:
6+
max_prod = nums[0]
7+
min_prod = nums[0]
8+
result = nums[0]
9+
10+
for i in range(1, len(nums)):
11+
candidates = (nums[i], nums[i] * max_prod, nums[i] * min_prod)
12+
max_prod, min_prod = max(candidates), min(candidates)
13+
result = max(result, max_prod)
14+
15+
return result
16+
17+
18+
19+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from typing import List
2+
from collections import deque
3+
4+
# time O(mn), saoce O(mn)
5+
class Solution:
6+
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
7+
m, n = len(heights), len(heights[0])
8+
# 방문 배열
9+
pacific = [[False] * n for _ in range(m)]
10+
atlantic = [[False] * n for _ in range(m)]
11+
12+
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
13+
14+
def bfs(r, c, visited):
15+
queue = deque([(r, c)])
16+
visited[r][c] = True
17+
18+
while queue:
19+
r, c = queue.popleft()
20+
21+
for dr, dc in directions:
22+
new_r, new_c = r + dr, c + dc
23+
if (0 <= new_r < m and 0 <= new_c < n
24+
and not visited[new_r][new_c]
25+
and heights[new_r][new_c] >= heights[r][c]):
26+
visited[new_r][new_c] = True
27+
queue.append((new_r, new_c))
28+
29+
for i in range(n):
30+
bfs(0, i, pacific)
31+
bfs(m - 1, i, atlantic)
32+
for i in range(m):
33+
bfs(i, 0, pacific)
34+
bfs(i, n - 1, atlantic)
35+
36+
result = []
37+
for i in range(m):
38+
for j in range(n):
39+
if pacific[i][j] and atlantic[i][j]:
40+
result.append([i, j])
41+
42+
return result
43+
44+
45+

0 commit comments

Comments
 (0)