Skip to content

Commit 7dd02b1

Browse files
authored
Merge pull request #863 from heypaprika/main
[croucs] Week : 5
2 parents 94908a3 + 4a9da1e commit 7dd02b1

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Big-O ์˜ˆ์ธก
2+
# Time : O(n)
3+
# Space : O(1)
4+
class Solution:
5+
def maxProfit(self, prices: List[int]) -> int:
6+
min_price = prices[0]
7+
profit = 0
8+
9+
for price in prices[1:]:
10+
if min_price > price:
11+
min_price = price
12+
13+
profit = max(profit, price - min_price)
14+
return profit
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Big-O ์˜ˆ์ธก
2+
# Time : O(n)
3+
# Space : O(1)
4+
class Codec:
5+
def encode(self, strs: List[str]) -> str:
6+
"""Encodes a list of strings to a single string.
7+
"""
8+
new_str = "-!@$@#!_".join(strs)
9+
return new_str
10+
11+
def decode(self, s: str) -> List[str]:
12+
"""Decodes a single string to a list of strings.
13+
"""
14+
return s.split("-!@$@#!_")
15+
16+
17+
18+
strs = ["Hello","World"]
19+
codec = Codec()
20+
codec.decode(codec.encode(strs))
21+

โ€Žgroup-anagrams/heypaprika.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Big-O ์˜ˆ์ธก
2+
# Time : O(k * nlog(n)) (k : strs ๋ฐฐ์—ด ์ˆ˜, n : strs ์›์†Œ์˜ unique ๋‹จ์–ด ๊ฐœ์ˆ˜)
3+
# Space : O(k + n)
4+
class Solution:
5+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
6+
count_dict = {}
7+
num = 0
8+
dicted_list = []
9+
for s in strs:
10+
cur = str(sorted(Counter(s).items()))
11+
dicted_list.append(cur)
12+
if cur in count_dict:
13+
continue
14+
count_dict[cur] = num
15+
num += 1
16+
17+
ans_list = [[] for _ in range(len(count_dict))]
18+
for k, v in count_dict.items():
19+
for i, dicted in enumerate(dicted_list):
20+
if dicted == k:
21+
ans_list[v].append(strs[i])
22+
return ans_list
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Big-O ์˜ˆ์ธก
2+
# Time : O(n) (n : ๋‹จ์–ด ๊ฐœ์ˆ˜)
3+
# Space : O(n)
4+
class Trie:
5+
6+
def __init__(self):
7+
self.trie_dict = {}
8+
self.trie_dict_list = [{}]
9+
10+
def insert(self, word: str) -> None:
11+
self.trie_dict[word] = 1
12+
for i in range(1, len(word) + 1):
13+
if i > len(self.trie_dict_list) and i > 1:
14+
self.trie_dict_list.append({})
15+
self.trie_dict_list[i-1][word[:i]] = 1
16+
17+
def search(self, word: str) -> bool:
18+
return word in self.trie_dict
19+
20+
def startsWith(self, prefix: str) -> bool:
21+
if len(prefix) > len(self.trie_dict_list):
22+
return False
23+
return prefix in self.trie_dict_list[len(prefix)-1]
24+
25+
26+
# Your Trie object will be instantiated and called as such:
27+
trie = Trie()
28+
trie.insert("apple")
29+
trie.search("apple")
30+
trie.search("app")
31+
trie.startsWith("app")
32+
trie.insert("app")
33+
trie.search("app")
34+
35+
# param_2 = trie.search(word)
36+
# param_3 = trie.startsWith(prefix)
37+

โ€Žword-break/heypaprika.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Big-O ์˜ˆ์ธก
2+
# Time : O(n^2) ์ตœ์•…์˜ ๊ฒฝ์šฐ wordDict์˜ ๊ฐœ์ˆ˜์˜ ์ œ๊ณฑ๋งŒํผ
3+
# Space : O(n^2) ์ตœ์•…์˜ ๊ฒฝ์šฐ a์— wordDict์˜ ๊ฐœ์ˆ˜์˜ ์ œ๊ณฑ๋งŒํผ
4+
5+
# ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•œ ํ’€์ด
6+
# ๋งจ ์•ž์˜ ๊ฒƒ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ๋‹ค ๊ณ ๋ฅด๊ธฐ (startswith)
7+
# ๊ทธ๋“ค์„ ํ–ˆ์„ ๋•Œ์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ  ๋‹ค์Œ ๊ฒƒ์œผ๋กœ ๋™์ผํ•˜๊ฒŒ ๋ฐ˜๋ณต ์ง„ํ–‰ํ•˜๊ธฐ.
8+
9+
class Solution:
10+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
11+
a = [[s]]
12+
i = 1
13+
s_list = None
14+
duplicate_dict = {}
15+
while True:
16+
s_list = a[-1]
17+
a.append([])
18+
for s in s_list:
19+
for word in wordDict:
20+
if s.startswith(word):
21+
surplus_word = s[len(word):]
22+
if surplus_word not in duplicate_dict:
23+
a[-1].append(surplus_word)
24+
duplicate_dict[surplus_word] = 1
25+
# a[-1] = list(set(a[-1]))
26+
if "" in a[-1]:
27+
return True
28+
if not a[-1]:
29+
return False
30+
i += 1
31+

0 commit comments

Comments
ย (0)