Skip to content

Commit aa22639

Browse files
authored
Merge pull request DaleStudy#982 from KwonNayeon/main
[KwonNayeon] Week 9
2 parents 532fe03 + 5140ffd commit aa22639

File tree

5 files changed

+239
-0
lines changed

5 files changed

+239
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Constraints:
3+
- n == nums.length
4+
- 1 <= n <= 5000
5+
- -5000 <= nums[i] <= 5000
6+
- All the integers of nums are unique.
7+
- nums is sorted and rotated between 1 and n times.
8+
9+
Time Complexity: O(log n)
10+
- ์ด์ง„ ํƒ์ƒ‰์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๋งค ๋‹จ๊ณ„๋งˆ๋‹ค ํƒ์ƒ‰ ๋ฒ”์œ„๊ฐ€ ์ ˆ๋ฐ˜์œผ๋กœ ์ค„์–ด๋“ฆ
11+
12+
Space Complexity: O(1)
13+
- ์ถ”๊ฐ€ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ํฌ์ธํ„ฐ๋งŒ ์‚ฌ์šฉ
14+
15+
ํ’€์ด๋ฐฉ๋ฒ•:
16+
1. ์ด์ง„ ํƒ์ƒ‰(Binary Search) ํ™œ์šฉ
17+
2. mid์™€ right ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ์กฐ๊ฑด์„ ๋‚˜๋ˆ”
18+
- Case 1: nums[mid] > nums[right]
19+
- ์˜ค๋ฅธ์ชฝ ๋ถ€๋ถ„์ด ์ •๋ ฌ๋˜์–ด ์žˆ์ง€ ์•Š์Œ
20+
- ์ตœ์†Ÿ๊ฐ’์€ mid ์˜ค๋ฅธ์ชฝ์— ์กด์žฌ
21+
- left = mid + 1
22+
- Case 2: nums[mid] <= nums[right]
23+
- mid๋ถ€ํ„ฐ right๊นŒ์ง€๋Š” ์ •๋ ฌ๋˜์–ด ์žˆ์Œ
24+
- ์ตœ์†Ÿ๊ฐ’์€ mid๋ฅผ ํฌํ•จํ•œ ์™ผ์ชฝ์— ์กด์žฌ ๊ฐ€๋Šฅ
25+
- right = mid
26+
"""
27+
28+
class Solution:
29+
def findMin(self, nums: List[int]) -> int:
30+
left = 0
31+
right = len(nums) - 1
32+
33+
while left < right:
34+
mid = (left + right) // 2
35+
36+
if nums[mid] > nums[right]:
37+
left = mid + 1
38+
39+
else:
40+
right = mid
41+
42+
return nums[left]

โ€Žlinked-list-cycle/KwonNayeon.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Constraints:
3+
- The number of the nodes in the list is in the range [0, 10^4]
4+
- -10^5 <= Node.val <= 10^5
5+
- pos is -1 or a valid index in the linked-list
6+
7+
Time Complexity:
8+
- Solution 1: O(n)
9+
- Solution 2: O(n)
10+
11+
Space Complexity:
12+
- Solution 1: O(n) - visited set์— ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Œ
13+
- Solution 2: O(1) - ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ
14+
15+
ํ’€์ด๋ฐฉ๋ฒ•:
16+
1. ์ฒ˜์Œ์—” ์ง๊ด€์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐ, ํ•œ ๋ฒˆ ๋งˆ์ฃผ์นœ ๋…ธ๋“œ๋ฅผ ๋‹ค์‹œ ๋งŒ๋‚˜๋Š”์ง€๋ฅผ ์ฒดํฌํ•˜๋Š” ๋ฐฉ์‹
17+
2. slow, fast ๋‘ ๊ฐœ์˜ ๋…ธ๋“œ๋ฅผ ํ™œ์šฉ, ๋งŒ์•ฝ cycle์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ fast๊ฐ€ slow์™€ ์–ธ์  ๊ฐ€ ๋งŒ๋‚˜๊ฒŒ ๋จ,
18+
๋งŒ์•ฝ cycle์ด ์—†๋‹ค๋ฉด ๋‘˜์€ ๋งŒ๋‚˜์ง€ ์•Š์Œ
19+
"""
20+
21+
# Definition for singly-linked list.
22+
# class ListNode:
23+
# def __init__(self, x):
24+
# self.val = x
25+
# self.next = None
26+
27+
# Solution 1: ํ•œ ๋ฒˆ ๋งˆ์ฃผ์นœ ๋…ธ๋“œ๋ฅผ ๋‹ค์‹œ ๋งŒ๋‚˜๋Š”์ง€๋ฅผ ์ฒดํฌ
28+
class Solution:
29+
def hasCycle(self, head: Optional[ListNode]) -> bool:
30+
visited = set()
31+
32+
current = head
33+
while current:
34+
if current in visited:
35+
return True
36+
visited.add(current)
37+
current = current.next
38+
39+
return False
40+
41+
# Solution 2: ๋‘ ๊ฐœ์˜ ํฌ์ธํ„ฐ ์ด์šฉ
42+
class Solution:
43+
def hasCycle(self, head: Optional[ListNode]) -> bool:
44+
if not head:
45+
return False
46+
47+
slow = head
48+
fast = head
49+
50+
while fast and fast.next:
51+
slow = slow.next
52+
fast = fast.next.next
53+
54+
if slow == fast:
55+
return True
56+
57+
return False
58+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Constraints:
3+
- 1 <= nums.length <= 2 * 10^4
4+
- -10 <= nums[i] <= 10
5+
- The product of any subarray of nums is guaranteed to fit in a 32-bit integer.
6+
7+
Time Complexity:
8+
- O(n): ๋ฐฐ์—ด์„ ํ•œ ๋ฒˆ๋งŒ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ์œ„์น˜์—์„œ ์ƒ์ˆ˜ ์‹œ๊ฐ„ ์—ฐ์‚ฐ๋งŒ ์ˆ˜ํ–‰
9+
10+
Space Complexity:
11+
- O(1): ๊ณ ์ •๋œ ์ถ”๊ฐ€ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉ (curr_max, curr_min, ...)
12+
13+
ํ’€์ด๋ฐฉ๋ฒ•:
14+
1. DP๋กœ ๊ฐ ์œ„์น˜์—์„œ ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€๊ณฑ๊ณผ ์ตœ์†Œ๊ณฑ์„ ๋™์‹œ์— ์ถ”์ ํ•จ
15+
2. ๊ฐ ์œ„์น˜์—์„œ ์„ธ ๊ฐœ์˜ ์„ ํƒ์ง€ ์กด์žฌ: ์ƒˆ๋กœ ์‹œ์ž‘ vs ์ด์ „ ์ตœ๋Œ€๊ณฑ๊ณผ ๊ณฑํ•˜๊ธฐ vs ์ด์ „ ์ตœ์†Œ๊ณฑ๊ณผ ๊ณฑํ•˜๊ธฐ
16+
3. ์ตœ์†Œ๊ณฑ์ด ํ•„์š”ํ•œ ์ด์œ : ๋‚˜์ค‘์— ์Œ์ˆ˜๋ฅผ ๋งŒ๋‚ฌ์„ ๋•Œ ์ตœ๋Œ€๊ฐ’์ด ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ
17+
4. ๋งค ๋‹จ๊ณ„๋งˆ๋‹ค result ์—…๋ฐ์ดํŠธ
18+
19+
๊ณ ๋ ค์‚ฌํ•ญ:
20+
1. ๊ฐ’์ด 0์ธ ๊ฒฝ์šฐ โ†’ ์ƒˆ๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•จ
21+
2. temp ๋ณ€์ˆ˜: curr_max๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋ฉด curr_min ๊ณ„์‚ฐ ์‹œ ์›๋ž˜ ๊ฐ’์ด ํ•„์š”ํ•จ
22+
"""
23+
class Solution:
24+
def maxProduct(self, nums: List[int]) -> int:
25+
curr_max = nums[0]
26+
curr_min = nums[0]
27+
result = nums[0]
28+
29+
for i in range(1, len(nums)):
30+
temp = curr_max
31+
curr_max = max(nums[i], curr_max * nums[i], curr_min * nums[i])
32+
curr_min = min(nums[i], temp * nums[i], curr_min * nums[i])
33+
result = max(result, curr_max)
34+
35+
return result
36+
37+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Constraints:
3+
- m == s.length
4+
- n == t.length
5+
- 1 <= m, n <= 10^5
6+
- s and t consist of uppercase and lowercase English letters.
7+
8+
Time Complexity: O(s * t)
9+
- for ๋ฃจํ”„๋กœ s์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณต
10+
- while ๋ฃจํ”„ ์•ˆ์˜ all() ์กฐ๊ฑด์—์„œ t์˜ ๊ฐ ๋ฌธ์ž ๋น„๊ต
11+
- ๋”ฐ๋ผ์„œ O(s * t)
12+
13+
Space Complexity: O(s + t)
14+
- t_counts: O(t)์˜ ๊ณต๊ฐ„
15+
- w_counts: O(s)์˜ ๊ณต๊ฐ„
16+
- ๋”ฐ๋ผ์„œ O(s + t)
17+
18+
ํ’€์ด๋ฐฉ๋ฒ•:
19+
1. Counter๋กœ t์˜ ๋ฌธ์ž ๋นˆ๋„์ˆ˜ ์ €์žฅ
20+
2. ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋กœ s ํƒ์ƒ‰:
21+
- high ํฌ์ธํ„ฐ๋กœ ์œˆ๋„์šฐ๋ฅผ ํ™•์žฅํ•˜๋ฉฐ ํ•„์š”ํ•œ ๋ฌธ์ž ์ฐพ๊ธฐ
22+
- ํ•„์š”ํ•œ ๋ฌธ์ž๋ฅผ ๋ชจ๋‘ ์ฐพ์œผ๋ฉด low ํฌ์ธํ„ฐ๋กœ ์œˆ๋„์šฐ ์ถ•์†Œ
23+
3. ์ตœ์†Œ ๊ธธ์ด์˜ ์œˆ๋„์šฐ ๋ฐ˜ํ™˜
24+
25+
๋ฉ”๋ชจ:
26+
- ํ’€์ด ๋ฐฉ๋ฒ•์„ ๋ณด๊ณ  ์ตํž˜
27+
- ํ•ด์‹œ๋งต๊ณผ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๊ด€๋ จ ๋‹ค๋ฅธ ๋ฌธ์ œ๋“ค์„ ํ’€๊ณ  ์ด ๋ฌธ์ œ ์Šค์Šค๋กœ ํ’€์–ด๋ณด๊ธฐ
28+
"""
29+
class Solution:
30+
def minWindow(self, s: str, t: str) -> str:
31+
32+
t_counts = Counter(t)
33+
w_counts = Counter()
34+
35+
min_low, min_high = 0, len(s)
36+
low = 0
37+
38+
for high in range(len(s)):
39+
w_counts[s[high]] += 1
40+
41+
while all(t_counts[ch] <= w_counts[ch] for ch in t_counts):
42+
if high - low < min_high - min_low:
43+
min_low, min_high = low, high
44+
if s[low] in t_counts:
45+
w_counts[s[low]] -= 1
46+
low += 1
47+
48+
return s[min_low : min_high + 1] if min_high < len(s) else ""
49+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Constraints:
3+
- m == heights.length
4+
- n == heights[r].length
5+
- 1 <= m, n <= 200
6+
- 0 <= heights[r][c] <= 10^5
7+
8+
Time Complexity: O(m*n)
9+
- ๊ฐ ์…€์„ ์ตœ๋Œ€ ํ•œ ๋ฒˆ์”ฉ๋งŒ ๋ฐฉ๋ฌธํ•จ
10+
11+
Space Complexity: O(m*n)
12+
- visited sets(pacific, atlantic)๊ฐ€ ์ตœ๋Œ€ m*n ํฌ๊ธฐ
13+
- ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ๋„ ์ตœ๋Œ€ m*n ๊นŠ์ด ๊ฐ€๋Šฅ
14+
15+
ํ’€์ด๋ฐฉ๋ฒ•:
16+
1. Pacific(์ขŒ์ธก์ƒ๋‹จ)๊ณผ Atlantic(์šฐ์ธกํ•˜๋‹จ)์˜ ๊ฒฝ๊ณ„์—์„œ ์‹œ์ž‘ํ•จ
17+
2. DFS๋กœ ํ˜„์žฌ ๋†’์ด๋ณด๋‹ค ๋†’๊ฑฐ๋‚˜ ๊ฐ™์€ ์ธ์ ‘ ์…€๋กœ๋งŒ ์ด๋™ํ•จ (๋ฌผ์€ ์œ„ -> ์•„๋ž˜๋กœ ํ๋ฅด์ง€๋งŒ, ๊ฑฐ๊พธ๋กœ ์ ‘๊ทผํ–ˆ์œผ๋‹ˆ๊นŒ)
18+
3. ๊ฐ ๋ฐ”๋‹ค์—์„œ ๋„๋‹ฌ ๊ฐ€๋Šฅํ•œ ์…€๋“ค์„ Set์— ์ €์žฅ
19+
4. ๋‘ Set์˜ ๊ต์ง‘ํ•ฉ์ด ์ •๋‹ต (์–‘์ชฝ ๋ฐ”๋‹ค๋กœ ๋ชจ๋‘ ํ๋ฅผ ์ˆ˜ ์žˆ๋Š” ์ง€์ ๋“ค)
20+
"""
21+
class Solution:
22+
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
23+
if not heights:
24+
return []
25+
26+
rows, cols = len(heights), len(heights[0])
27+
pacific = set()
28+
atlantic = set()
29+
30+
def dfs(r, c, visited):
31+
visited.add((r, c))
32+
directions = [(1,0), (-1,0), (0,1), (0,-1)]
33+
34+
for dr, dc in directions:
35+
new_r, new_c = r + dr, c + dc
36+
if (new_r < 0 or new_r >= rows or
37+
new_c < 0 or new_c >= cols or
38+
(new_r, new_c) in visited):
39+
continue
40+
if heights[new_r][new_c] >= heights[r][c]:
41+
dfs(new_r, new_c, visited)
42+
43+
for c in range(cols):
44+
dfs(0, c, pacific)
45+
for r in range(rows):
46+
dfs(r, 0, pacific)
47+
48+
for c in range(cols):
49+
dfs(rows-1, c, atlantic)
50+
for r in range(rows):
51+
dfs(r, cols-1, atlantic)
52+
53+
return list(pacific & atlantic)

0 commit comments

Comments
ย (0)