Skip to content

Commit fb89135

Browse files
authoredFeb 9, 2025
Merge pull request #995 from pmjuu/main
[Lyla] Week 09
2 parents 18b2713 + 4246af3 commit fb89135

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
시간 복잡도: O(log n)
3+
- 이진 탐색을 사용하여 매 반복마다 검색 범위를 절반으로 줄이므로 O(log n)입니다.
4+
5+
공간 복잡도: O(1)
6+
- 추가적인 배열이나 리스트를 사용하지 않고, 몇 개의 변수만 사용하므로 O(1)입니다.
7+
'''
8+
9+
from typing import List
10+
11+
class Solution:
12+
def findMin(self, nums: List[int]) -> int:
13+
left, right = 0, len(nums) - 1
14+
15+
while left < right:
16+
mid = (left + right) // 2
17+
if nums[mid] > nums[right]:
18+
left = mid + 1 # 최소값이 오른쪽에 있음
19+
else:
20+
right = mid # 최소값이 mid 또는 왼쪽에 있음
21+
22+
return nums[left]

‎linked-list-cycle/pmjuu.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
시간 복잡도: O(n)
3+
- `fast`와 `slow` 포인터가 리스트를 한 번 순회하면서 주어진 연결 리스트의 길이에 비례하는 작업을 수행합니다.
4+
- 따라서 최악의 경우 모든 노드를 한 번씩 방문하게 되므로 O(n)입니다.
5+
6+
공간 복잡도: O(1)
7+
- 추가적인 자료구조를 사용하지 않고, `fast`와 `slow`라는 두 개의 포인터만 사용하므로 O(1)입니다.
8+
'''
9+
from typing import Optional
10+
# Definition for singly-linked list.
11+
class ListNode:
12+
def __init__(self, x):
13+
self.val = x
14+
self.next = None
15+
16+
class Solution:
17+
def hasCycle(self, head: Optional[ListNode]) -> bool:
18+
fast = head
19+
slow = head
20+
21+
while fast and fast.next:
22+
fast = fast.next.next
23+
slow = slow.next
24+
25+
if fast == slow:
26+
return True
27+
28+
return False

‎maximum-product-subarray/pmjuu.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
시간 복잡도: O(n)
3+
- 리스트를 한 번 순회하면서 각 요소에 대해 최대값과 최소값을 갱신하므로 O(n)입니다.
4+
5+
공간 복잡도: O(1)
6+
- 추가적인 배열을 사용하지 않고, 몇 개의 변수만 사용하므로 O(1)입니다.
7+
'''
8+
9+
from typing import List
10+
11+
class Solution:
12+
def maxProduct(self, nums: List[int]) -> int:
13+
n = len(nums)
14+
max_product = nums[0]
15+
cur_max = nums[0] # 현재 위치까지 최대 곱
16+
cur_min = nums[0] # 현재 위치까지 최소 곱 (음수 대비)
17+
18+
for i in range(1, n):
19+
temp_max = cur_max
20+
cur_max = max(nums[i], cur_max * nums[i], cur_min * nums[i])
21+
cur_min = min(nums[i], temp_max * nums[i], cur_min * nums[i])
22+
max_product = max(max_product, cur_max)
23+
24+
return max_product

‎pacific-atlantic-water-flow/pmjuu.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'''
2+
시간 복잡도: O(m * n)
3+
- 각 바다에서 BFS를 한 번씩 수행하며, 각 셀을 최대 한 번씩 방문하므로 O(m * n)입니다.
4+
5+
공간 복잡도: O(m * n)
6+
- BFS 탐색을 위한 큐와 방문한 셀을 저장하는 집합(set)이 필요하므로 O(m * n)입니다.
7+
'''
8+
9+
from collections import deque
10+
from typing import List
11+
12+
class Solution:
13+
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
14+
m, n = len(heights), len(heights[0])
15+
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
16+
17+
def bfs(starts):
18+
queue = deque(starts)
19+
reachable = set(starts)
20+
21+
while queue:
22+
r, c = queue.popleft()
23+
for dr, dc in directions:
24+
nr, nc = r + dr, c + dc
25+
if (0 <= nr < m and 0 <= nc < n and
26+
(nr, nc) not in reachable and
27+
heights[nr][nc] >= heights[r][c]): # 물이 흐를 수 있는지 확인
28+
queue.append((nr, nc))
29+
reachable.add((nr, nc))
30+
31+
return reachable
32+
33+
# 태평양(왼쪽과 위쪽 가장자리)에서 시작하는 셀들
34+
pacific_starts = [(0, c) for c in range(n)] + [(r, 0) for r in range(m)]
35+
# 대서양(오른쪽과 아래쪽 가장자리)에서 시작하는 셀들
36+
atlantic_starts = [(m-1, c) for c in range(n)] + [(r, n-1) for r in range(m)]
37+
38+
pacific_reach = bfs(pacific_starts)
39+
atlantic_reach = bfs(atlantic_starts)
40+
41+
return list(pacific_reach & atlantic_reach)
42+
43+
44+
class Solution:
45+
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
46+
if not heights or not heights[0]:
47+
return []
48+
49+
m, n = len(heights), len(heights[0])
50+
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
51+
52+
def dfs(r, c, reachable):
53+
reachable.add((r, c))
54+
for dr, dc in directions:
55+
nr, nc = r + dr, c + dc
56+
if (0 <= nr < m and 0 <= nc < n and
57+
(nr, nc) not in reachable and
58+
heights[nr][nc] >= heights[r][c]):
59+
dfs(nr, nc, reachable)
60+
61+
pacific_reach, atlantic_reach = set(), set()
62+
63+
for c in range(n):
64+
dfs(0, c, pacific_reach)
65+
dfs(m-1, c, atlantic_reach)
66+
67+
for r in range(m):
68+
dfs(r, 0, pacific_reach)
69+
dfs(r, n-1, atlantic_reach)
70+
71+
return list(pacific_reach & atlantic_reach)

0 commit comments

Comments
 (0)
Please sign in to comment.