Skip to content

Commit 8cab3e1

Browse files
authored
Merge pull request #2179 from devyulbae/main
[devyulbae] Week 04~05 solutions
2 parents 465ae3b + 8b7806a commit 8cab3e1

File tree

11 files changed

+268
-1
lines changed

11 files changed

+268
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Blind 75 - Best Time to Buy and Sell Stock
3+
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
4+
5+
์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
6+
๊ณต๊ฐ„๋ณต์žก๋„ : O(1)
7+
ํ’€์ด : ํ•œ ๋ฒˆ์˜ ์ˆœํšŒ๋กœ ํ•ด๊ฒฐ
8+
9+
prices = [7,1,5,3,6,4]
10+
11+
price | min | max_profit
12+
7 | 7 | 0
13+
1 | 1 | 0
14+
5 | 1 | 4
15+
3 | 1 | 4
16+
6 | 1 | 5
17+
4 | 1 | 5
18+
19+
"""
20+
from typing import List
21+
22+
class Solution:
23+
def maxProfit(self, prices: List[int]) -> int:
24+
max_profit = 0
25+
min_price = prices[0]
26+
27+
for price in prices:
28+
min_price = min(min_price, price)
29+
max_profit = max(max_profit, price - min_price)
30+
31+
return max_profit
32+

โ€Žcoin-change/devyulbae.pyโ€Ž

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Blind 75 - Coin Change
3+
LeetCode Problem Link: https://leetcode.com/problems/coin-change/
4+
์‹œ๊ฐ„๋ณต์žก๋„ : O(n*m) (n: amount, m: len(coins))
5+
๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
6+
7+
"""
8+
from typing import List
9+
class Solution:
10+
def coinChange(self, coins: List[int], amount: int) -> int:
11+
dp = [float('inf')] * (amount + 1)
12+
dp[0] = 0
13+
14+
for coin in coins: # ๊ฐ ๋™์ „๋งˆ๋‹ค
15+
for x in range(coin, amount + 1): # ๊ธˆ์•ก 1 coin๋ถ€ํ„ฐ amount๊นŒ์ง€
16+
dp[x] = min(dp[x], dp[x - coin] + 1) # ํ•ด๋‹น ๊ธˆ์•ก์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์ตœ์†Œ ๋™์ „ ๊ฐœ์ˆ˜ ๊ฐฑ์‹ 
17+
18+
return dp[amount] if dp[amount] != float('inf') else -1 # amount๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ -1 ๋ฐ˜ํ™˜
19+

โ€Ždecode-ways/devyulbae.pyโ€Ž

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1+
"""
2+
Blind75 - Decode Ways
3+
https://leetcode.com/problems/decode-ways/
4+
์‹œ๊ฐ„๋ณต์žก๋„: O(n)
5+
๊ณต๊ฐ„๋ณต์žก๋„: O(n)
6+
ํ’€์ด : ๋ฉ”๋ชจ์ด์ œ์ด์…˜ + DFS๋ฅผ ํ™œ์šฉํ•œ ์žฌ๊ท€ ํ’€์ด
7+
8+
์•„..DP๋กœ๋Š” ๋ชป ํ’€๊ฒ ์–ด์„œ ํ’€์ด ๋ดค์Šต๋‹ˆ๋‹ค. ๊ฐ•์˜๋‚˜ ์ฑ…์„ ๋ด์•ผ๊ฒ ๊ตฐ์š”
19
class Solution:
210
def numDecodings(self, s: str) -> int:
3-
11+
dp = [0] * len(s) + [1]
12+
for start in reversed(range(len(s))):
13+
if s[start] == "0":
14+
dp[start] = 0
15+
elif start + 1 < len(s) and int(s[start : start + 2]) < 27:
16+
dp[start] = dp[start + 1] + dp[start + 2]
17+
else:
18+
dp[start] = dp[start + 1]
19+
return dp[0]
20+
"""
21+
class Solution:
22+
def numDecodings(self, s: str) -> int:
23+
memo = {len(s): 1}
24+
25+
def dfs(start):
26+
if start in memo:
27+
return memo[start]
28+
29+
if s[start] == "0":
30+
memo[start] = 0
31+
elif start + 1 < len(s) and int(s[start : start + 2]) < 27:
32+
memo[start] = dfs(start + 1) + dfs(start + 2)
33+
else:
34+
memo[start] = dfs(start + 1)
35+
return memo[start]
36+
37+
return dfs(0)
38+
39+

โ€Žencode-and-decode-strings/devyulbae.pyโ€Ž

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Blind 75 - Find Minimum in Rotated Sorted Array
3+
LeetCode Problem Link: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
4+
์‹œ๊ฐ„๋ณต์žก๋„ : O(log n)
5+
๊ณต๊ฐ„๋ณต์žก๋„ : O(1)
6+
ํ’€์ด : ์ด์ง„ ํƒ์ƒ‰
7+
์ค‘๊ฐ„์  ๊ธฐ์ค€์œผ๋กœ nums[mid-1] > nums[mid] ์ด๋ฉด mid๊ฐ€ ์ตœ์†Œ๊ฐ’
8+
๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์™ผ์ชฝ ์ ˆ๋ฐ˜์ด ์ •๋ ฌ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์„œ ์™ผ์ชฝ ์ ˆ๋ฐ˜ or ์˜ค๋ฅธ์ชฝ ์ ˆ๋ฐ˜์œผ๋กœ ํƒ์ƒ‰ ๋ฒ”์œ„๋ฅผ ์ขํ˜€๋‚˜๊ฐ
9+
๋งŒ์•ฝ ๋ฐœ๊ฒฌ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด nums[0]์ด ์ตœ์†Œ๊ฐ’
10+
"""
11+
from typing import List
12+
class Solution:
13+
def findMin(self, nums: List[int]) -> int:
14+
left, right = 1, len(nums) - 1
15+
while left <= right:
16+
mid = (left + right) // 2
17+
if nums[mid -1] > nums[mid]:
18+
return nums[mid]
19+
if nums[0] < nums[mid]:
20+
left = mid + 1
21+
else:
22+
right = mid - 1
23+
return nums[0]
24+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Blind 75 - Group Anagrams
3+
LeetCode Problem: https://leetcode.com/problems/group-anagrams/
4+
์‹œ๊ฐ„๋ณต์žก๋„ : O(N * K) (N์€ strs์˜ ๊ธธ์ด, K๋Š” strs ๋‚ด ๋ฌธ์ž์—ด์˜ ์ตœ๋Œ€ ๊ธธ์ด)
5+
๊ณต๊ฐ„๋ณต์žก๋„ : O(N * K)
6+
ํ’€์ด : ํ•ด์‹œ๋งต์„ ์ด์šฉํ•œ ํ’€์ด
7+
ASCII ๋ฌธ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด ์ด๋ฅผ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด์‹œ๋งต์— ์ €์žฅํ•œ๋‹ค
8+
9+
"""
10+
11+
from typing import List
12+
from collections import defaultdict
13+
class Solution:
14+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
15+
anagrams = defaultdict(list)
16+
17+
for s in strs:
18+
count = [0] * 26
19+
for char in s:
20+
count[ord(char) - ord('a')] += 1
21+
anagrams[tuple(count)].append(s)
22+
23+
return list(anagrams.values())
24+

โ€Žimplement-trie-prefix-tree/devyulbae.pyโ€Ž

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Blind 75 - Maximum Depth of Binary Tree
3+
LeetCode Problem Link: https://leetcode.com/problems/maximum-depth-of-binary-tree/
4+
์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
5+
๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
6+
ํ’€์ด : ์žฌ๊ท€๋ฅผ ์ด์šฉํ•œ ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS)
7+
์ž์‹ ๋…ธ๋“œ๋ถ€ํ„ฐ ๋ถ€๋ชจ ๋…ธ๋“œ๋กœ ๊ฑฐ์Šฌ๋Ÿฌ ์˜ฌ๋ผ๊ฐ€๋ฉฐ ์ตœ๋Œ€ ๊นŠ์ด๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
8+
์ข…๋ฃŒ ์กฐ๊ฑด - left์™€ right๊ฐ€ ๋ชจ๋‘ None์ธ ๊ฒฝ์šฐ (1+max(0,0))์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ์žฌ๊ท€๊ฐ€ ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.
9+
"""
10+
11+
from typing import Optional
12+
# Definition for a binary tree node.
13+
class TreeNode:
14+
def __init__(self, val=0, left=None, right=None):
15+
self.val = val
16+
self.left = left
17+
self.right = right
18+
class Solution:
19+
def maxDepth(self, root: Optional[TreeNode]) -> int:
20+
if not root:
21+
return 0
22+
23+
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
24+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Blind 75 - Merge Two Sorted Lists
3+
LeetCode Problem Link: https://leetcode.com/problems/merge-two-sorted-lists/
4+
์‹œ๊ฐ„๋ณต์žก๋„ : O(n+m)
5+
๊ณต๊ฐ„๋ณต์žก๋„ : O(1)
6+
ํ’€์ด : (์•Œ๊ณ ๋‹ฌ๋ ˆ ๋‹ต์•ˆ)
7+
๊ฐ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์— ํฌ์ธํ„ฐ๋ฅผ ๋‘๊ณ , ๋” ์ž‘์€ ๊ฐ’์„ ๊ฐ€์ง„ ๋…ธ๋“œ๋ฅผ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•˜๋‹ค๊ฐ€ ํ•œ ์ชฝ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋๋‚˜๋ฉด ๋‚˜๋จธ์ง€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•œ๋‹ค.
8+
๋งˆ์ง€๋ง‰์— ์ฒซ๋ฒˆ์งธ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
9+
"""
10+
from typing import Optional
11+
12+
# Definition for singly-linked list.
13+
class ListNode:
14+
def __init__(self, val=0, next=None):
15+
self.val = val
16+
self.next = next
17+
18+
class Solution:
19+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
20+
dummy = ListNode()
21+
tail = dummy
22+
23+
while list1 and list2:
24+
if list1.val < list2.val:
25+
tail.next = list1
26+
list1 = list1.next
27+
else:
28+
tail.next = list2
29+
list2 = list2.next
30+
31+
tail = tail.next
32+
33+
tail.next = list1 or list2
34+
return dummy.next
35+

โ€Žword-break/devyulbae.pyโ€Ž

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Blind 75 - Word Break
3+
LeetCode Problem: https://leetcode.com/problems/word-break/
4+
์‹œ๊ฐ„๋ณต์žก๋„ : O(n^2)
5+
๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
6+
ํ’€์ด : ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ด์šฉํ•œ ํ’€์ด
7+
๋ฌธ์ž์—ด s์˜ ๊ธธ์ด๋ฅผ n์ด๋ผ ํ•  ๋•Œ, dp[i]๋ฅผ s์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ i-1๋ฒˆ์งธ ๋ฌธ์ž๊นŒ์ง€์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด
8+
๋‹จ์–ด ์‚ฌ์ „์— ์žˆ๋Š” ๋‹จ์–ด๋“ค๋กœ ๊ตฌ์„ฑ๋  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์ด๋ผ๊ณ  ์ •์˜ํ•œ๋‹ค.
9+
"""
10+
from typing import List
11+
class Solution:
12+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
13+
dp = [False] * (len(s) + 1)
14+
dp[0] = True
15+
16+
word_set = set(wordDict)
17+
for i in range(1, len(s) + 1):
18+
for j in range(i):
19+
if dp[j] and s[j:i] in word_set: # 0~j-1๊นŒ์ง€ ๋ฌธ์ž์—ด์ด ๋‹จ์–ด์‚ฌ์ „์— ์žˆ๊ณ , j~i-1๊นŒ์ง€ ๋ฌธ์ž์—ด์ด ๋‹จ์–ด์‚ฌ์ „์— ์žˆ๋Š” ๊ฒฝ์šฐ
20+
dp[i] = True
21+
break
22+
23+
return dp[len(s)]

0 commit comments

Comments
ย (0)