Skip to content

Commit 0add5a1

Browse files
authored
Merge pull request #2502 from sangbeenmoon/main
[sangbeenmoon] WEEK 05 Solutions
2 parents a01ec4a + 4e7811f commit 0add5a1

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# TC : O(n)
2+
# SC : O(1)
3+
4+
class Solution:
5+
def maxProfit(self, prices: List[int]) -> int:
6+
answer = 0
7+
mm = 10001
8+
9+
for price in prices:
10+
mm = min(mm, price)
11+
answer = max(answer, price - mm)
12+
13+
return answer
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
# write your code here
8+
result = ""
9+
10+
delimiter = ":;"
11+
12+
for i, str in enumerate(strs):
13+
result = result + str
14+
if i != len(strs):
15+
result = result + delimiter
16+
17+
print(result)
18+
19+
return result
20+
21+
"""
22+
@param: str: A string
23+
@return: decodes a single string to a list of strings
24+
"""
25+
def decode(self, str):
26+
result = []
27+
28+
i = 0
29+
delimiter = ":;"
30+
31+
while i < len(str):
32+
33+
j = i
34+
while j < len(str) - 1:
35+
if str[j:j+2] == delimiter:
36+
result.append(str[i:j])
37+
break
38+
else:
39+
j = j + 1
40+
41+
i = j + 2
42+
43+
return result
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# TC : O(m^2 * n) m : len(strs), n : len(strs[0])
2+
# SC : O(m * n)
3+
4+
class Solution:
5+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
6+
sorted_strs = []
7+
for str in strs:
8+
sorted_strs.append(sorted(str))
9+
10+
answer = []
11+
12+
i = 0
13+
14+
visited = [False] * 10001
15+
16+
while i < len(strs):
17+
if visited[i]:
18+
i = i + 1
19+
continue
20+
21+
sub_answer = []
22+
target = sorted_strs[i]
23+
sub_answer.append(strs[i])
24+
25+
for j in range(i+1, len(strs)):
26+
if sorted_strs[j] == target:
27+
visited[j] = True
28+
sub_answer.append(strs[j])
29+
30+
i = i + 1
31+
answer.append(sub_answer)
32+
33+
return answer
34+
35+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
class Trie {
5+
6+
Map<Character, Trie> map;
7+
boolean isEnd = false;
8+
9+
public Trie() {
10+
map = new HashMap<>();
11+
}
12+
13+
public void insert(String word) {
14+
Trie trie;
15+
if (map.containsKey(word.charAt(0))) {
16+
trie = map.get(word.charAt(0));
17+
} else {
18+
trie = new Trie();
19+
}
20+
map.put(word.charAt(0), trie);
21+
22+
if (word.length() == 1) {
23+
map.get(word.charAt(0)).isEnd = true;
24+
return;
25+
}
26+
27+
trie.insert(word.substring(1));
28+
}
29+
30+
public boolean search(String word) {
31+
if (map.containsKey(word.charAt(0))) {
32+
if (word.length() == 1) {
33+
return map.get(word.charAt(0)).isEnd;
34+
}
35+
return map.get(word.charAt(0)).search(word.substring(1));
36+
}
37+
return false;
38+
}
39+
40+
public boolean startsWith(String prefix) {
41+
if (map.containsKey(prefix.charAt(0))) {
42+
if (prefix.length() == 1) {
43+
return true;
44+
}
45+
return map.get(prefix.charAt(0)).startsWith(prefix.substring(1));
46+
}
47+
return false;
48+
}
49+
}
50+
51+
/**
52+
* Your Trie object will be instantiated and called as such:
53+
* Trie obj = new Trie();
54+
* obj.insert(word);
55+
* boolean param_2 = obj.search(word);
56+
* boolean param_3 = obj.startsWith(prefix);
57+
*/

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# ์‹คํŒจํ•œ ํ’€์ด.
2+
# AC ๋ฅผ ๋ฐ›๊ธฐ๋Š” ํ–ˆ์œผ๋‚˜ test case ๊ฐ€ ์ข€ ๋” ์ด˜์ด˜ํ–ˆ๋‹ค๋ฉด TLE ์— ๊ฑธ๋ ธ์„ ๊ฒƒ์ž„.
3+
# string ์ด ์•„๋‹Œ index ๋กœ memoization ์„ ํ•˜๋Š” ๊ฑธ ๋– ์˜ฌ๋ ค๋ณด์ž.
4+
5+
class Solution:
6+
answer = False
7+
visited = {}
8+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
9+
10+
self.answer = False
11+
self.visited = {}
12+
self.go(0,s,wordDict)
13+
14+
return self.answer
15+
16+
def go(self, i:int, s: str, wordDict: List[str]):
17+
if i >= (len(s)):
18+
self.answer = True
19+
return
20+
21+
for word in wordDict:
22+
if i + len(word) > len(s):
23+
continue
24+
25+
if word == s[i:i + len(word)]:
26+
if not s[i + len(word) : ] in self.visited:
27+
self.go(i + len(word), s, wordDict)
28+
29+
self.visited[s[i:]] = False

0 commit comments

Comments
ย (0)