Skip to content

Commit 194fda6

Browse files
authored
Merge pull request #1840 from hu6r1s/main
[hu6r1s] WEEK 05 Solutions
2 parents b1e9f43 + 12d1fc0 commit 194fda6

File tree

6 files changed

+119
-0
lines changed

6 files changed

+119
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
"""
3+
1. ๋ธŒ๋ฃจํŠธํฌ์Šค
4+
2์ค‘ for๋ฌธ์ด๋ผ์„œ O(n^2)์ž„.
5+
prices์˜ ๊ธธ์ด๊ฐ€ 10^5์ด๋ฏ€๋กœ 10^10์ด ๋˜๋ฉด์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒ
6+
7+
2. ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ ๊ฐ€๋Šฅํ• ๊นŒ ํ–ˆ์ง€๋งŒ DP๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ ๊ฐ™์Œ
8+
์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n)์ด ๋‚˜์˜ด
9+
"""
10+
"""
11+
def maxProfit(self, prices: List[int]) -> int:
12+
max_profit = 0
13+
for i in range(len(prices)-1):
14+
for j in range(i, len(prices)):
15+
profit = prices[j] - prices[i]
16+
max_profit = max(max_profit, profit)
17+
return max_profit
18+
"""
19+
def maxProfit(self, prices: List[int]) -> int:
20+
max_profit = 0
21+
min_price = prices[0]
22+
for price in prices:
23+
max_profit = max(max_profit, price - min_price)
24+
min_price = min(price, min_price)
25+
return max_profit
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
"""
3+
@param: strs: a list of strings
4+
@return: encodes a list of strings to a single string.
5+
"""
6+
def encode(self, strs):
7+
return "secretKey!@#".join(strs)
8+
9+
"""
10+
@param: str: A string
11+
@return: decodes a single string to a list of strings
12+
"""
13+
def decode(self, str):
14+
return str.split("secretKey!@#")

โ€Žgroup-anagrams/hu6r1s.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from collections import defaultdict
2+
3+
class Solution:
4+
"""
5+
1. ์ •๋ ฌ๋œ ๊ฐ’์ด ๋”•์…”๋„ˆ๋ฆฌ์— ์žˆ์œผ๋ฉด ๋ฆฌ์ŠคํŠธ ํ˜•์‹์œผ๋กœ ์‚ฝ์ž…
6+
"""
7+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
8+
dict_strs = defaultdict(list)
9+
10+
for word in strs:
11+
dict_strs[str(sorted(word))].append(word)
12+
return list(dict_strs.values())
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Trie:
2+
3+
def __init__(self):
4+
self.root = {"$": True}
5+
6+
def insert(self, word: str) -> None:
7+
node = self.root
8+
for ch in word:
9+
if ch not in node:
10+
node[ch] = {"$": False}
11+
node = node[ch]
12+
node["$"] = True
13+
14+
def search(self, word: str) -> bool:
15+
node = self.root
16+
for ch in word:
17+
if ch not in node:
18+
return False
19+
node = node[ch]
20+
return node["$"]
21+
22+
def startsWith(self, prefix: str) -> bool:
23+
node = self.root
24+
for ch in prefix:
25+
if ch not in node:
26+
return False
27+
node = node[ch]
28+
return True
29+
30+
31+
# Your Trie object will be instantiated and called as such:
32+
# obj = Trie()
33+
# obj.insert(word)
34+
# param_2 = obj.search(word)
35+
# param_3 = obj.startsWith(prefix)

โ€Žword-break/hu6r1s.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public boolean wordBreak(String s, List<String> wordDict) {
5+
Set<String> wordSet = new HashSet<>(wordDict);
6+
boolean[] dp = new boolean[s.length() + 1];
7+
dp[0] = true;
8+
9+
for (int i = 1; i < s.length() + 1; i++) {
10+
for (int j = 0; j < i; j++) {
11+
if (dp[j] && (wordSet.contains(s.substring(j, i)))) {
12+
dp[i] = true;
13+
break;
14+
}
15+
}
16+
}
17+
return dp[s.length()];
18+
}
19+
}

โ€Žword-break/hu6r1s.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
3+
word_set = set(wordDict) # O(1) ์กฐํšŒ๋ฅผ ์œ„ํ•ด set์œผ๋กœ ๋ณ€ํ™˜
4+
n = len(s)
5+
dp = [False] * (n + 1)
6+
dp[0] = True # ๊ณต์ง‘ํ•ฉ์€ ํ•ญ์ƒ ๊ฐ€๋Šฅ
7+
8+
for i in range(1, n + 1):
9+
for j in range(i):
10+
if dp[j] and s[j:i] in word_set:
11+
dp[i] = True
12+
break # i๋ฒˆ์งธ๊นŒ์ง€ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์œผ๋ฉด ๋” ํ™•์ธํ•  ํ•„์š” ์—†์Œ
13+
14+
return dp[-1]

0 commit comments

Comments
ย (0)