Skip to content

Commit d861e7d

Browse files
authored
Merge pull request #1916 from hu6r1s/main
[hu6r1s] WEEK 09 Solutions
2 parents d7e778f + 3425a8a commit d861e7d

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

linked-list-cycle/hu6r1s.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
"""
9+
생각보다 쉬운 문제.
10+
"""
11+
def hasCycle(self, head: Optional[ListNode]) -> bool:
12+
visited = set()
13+
while head:
14+
if head in visited:
15+
return True
16+
visited.add(head)
17+
head = head.next
18+
return False

maximum-product-subarray/hu6r1s.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def maxProduct(self, nums: List[int]) -> int:
3+
result = nums[0]
4+
min_prod = max_prod = 1
5+
for num in nums:
6+
min_prod, max_prod = min(min_prod * num, max_prod * num, num), max(min_prod * num, max_prod * num, num)
7+
result = max(max_prod, result)
8+
return result

minimum-window-substring/hu6r1s.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution:
2+
"""
3+
접근 방법: https://www.youtube.com/watch?v=jSto0O4AJbM
4+
- t의 문자 개수를 미리 세어둔 후(t_count),
5+
s에서 두 포인터(left, right)를 이용해 슬라이딩 윈도우를 확장/축소하며
6+
모든 조건을 만족하는 최소 구간을 찾는다.
7+
- have: 현재 window에서 조건을 만족한 문자 종류 개수
8+
- need: t에 있는 문자 종류 개수
9+
- 조건을 만족할 때마다 왼쪽 포인터를 줄여 최소 길이 갱신
10+
11+
시간복잡도:
12+
- 각 문자를 오른쪽 포인터로 한 번, 왼쪽 포인터로 한 번만 방문 → O(|s|)
13+
- t의 해시맵 구성 O(|t|)
14+
- 최종 시간복잡도 = O(|s| + |t|)
15+
16+
공간복잡도:
17+
- window와 t_count 해시맵 → O(|s| + |t|)
18+
"""
19+
def minWindow(self, s: str, t: str) -> str:
20+
t_count, window = {}, {}
21+
22+
for c in t:
23+
t_count[c] = 1 + t_count.get(c, 0)
24+
25+
have, need = 0, len(t_count)
26+
res, len_res = [-1, -1], float("infinity")
27+
left = 0
28+
for right in range(len(s)):
29+
c = s[right]
30+
window[c] = 1 + window.get(c, 0)
31+
32+
if c in t_count and window[c] == t_count[c]:
33+
have += 1
34+
35+
while have == need:
36+
if (right - left + 1) < len_res:
37+
res = [left, right]
38+
len_res = (right - left + 1)
39+
window[s[left]] -= 1
40+
if s[left] in t_count and window[s[left]] < t_count[s[left]]:
41+
have -= 1
42+
left += 1
43+
left, right = res
44+
return s[left:right+1] if len_res != float("infinity") else ""
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
"""
3+
https://www.youtube.com/watch?v=s-VkcjHqkGI
4+
그래프 문제여서 바로 dfs나 bfs로 불면 될 것 같다는 생각이 들었는데 어떻게 해야할지 막막했다. 영상의 방식을 보고 공부
5+
"""
6+
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
7+
rows, cols = len(heights), len(heights[0])
8+
pac, atl = set(), set()
9+
10+
def dfs(r, c, visit, prevHeight):
11+
if ((r, c) in visit or r < 0 or c < 0 or r == rows or c == cols or heights[r][c] < prevHeight):
12+
return
13+
visit.add((r, c))
14+
dfs(r + 1, c, visit, heights[r][c])
15+
dfs(r - 1, c, visit, heights[r][c])
16+
dfs(r, c + 1, visit, heights[r][c])
17+
dfs(r, c - 1, visit, heights[r][c])
18+
19+
20+
for c in range(cols):
21+
dfs(0, c, pac, heights[0][c])
22+
dfs(rows - 1, c, atl, heights[rows-1][c])
23+
24+
for r in range(rows):
25+
dfs(r, 0, pac, heights[r][0])
26+
dfs(r, cols - 1, atl, heights[r][cols - 1])
27+
28+
result = []
29+
for r in range(rows):
30+
for c in range(cols):
31+
if (r, c) in pac and (r, c) in atl:
32+
result.append([r, c])
33+
return results

sum-of-two-integers/hu6r1s.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def getSum(self, a: int, b: int) -> int:
3+
mask = 0xFFFFFFFF
4+
while b & mask:
5+
a, b = a ^ b, (a & b) << 1
6+
return (a & mask) if b > 0 else a

0 commit comments

Comments
 (0)