Skip to content

Commit e537a6e

Browse files
authored
Merge pull request #883 from HodaeSsi/main
[HodaeSsi] Week 5
2 parents fddf7c3 + 5de30ec commit e537a6e

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 시간 복잡도 : O(n)
2+
# 공간 복잡도 : O(1)
3+
class Solution:
4+
def maxProfit(self, prices: List[int]) -> int:
5+
min_price = float('inf')
6+
max_profit = 0
7+
8+
for price in prices:
9+
min_price = min(min_price, price)
10+
max_profit = max(max_profit, price - min_price)
11+
12+
return max_profit
13+

encode-and-decode-strings/HodaeSsi.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 시간복잡도: O(n)
2+
# 공간복잡도: O(1)
3+
4+
class Solution:
5+
"""
6+
@param: strs: a list of strings
7+
@return: encodes a list of strings to a single string.
8+
"""
9+
def encode(self, strs):
10+
if not strs:
11+
return ""
12+
return chr(257).join(strs)
13+
14+
"""
15+
@param: str: A string
16+
@return: decodes a single string to a list of strings
17+
"""
18+
def decode(self, str):
19+
if not str:
20+
return []
21+
return str.split(chr(257))
22+

group-anagrams/HodaeSsi.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 시간복잡도 : O(n*mlogm) (n은 strs의 길이, m은 strs의 원소의 길이)
2+
# 공간복잡도 : O(n)
3+
4+
class Solution:
5+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
6+
anagrams = collections.defaultdict(list)
7+
for word in strs:
8+
anagrams[''.join(sorted(word))].append(word)
9+
return list(anagrams.values())
10+
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 시간복잡도: O(n) (n은 문자열의 길이)
2+
# 공간복잡도: O(n) (n은 문자열의 길이)
3+
class Trie:
4+
def __init__(self):
5+
self.root = {}
6+
self.end = False
7+
8+
def insert(self, word: str) -> None:
9+
node = self.root
10+
for char in word:
11+
if char not in node:
12+
node[char] = {}
13+
node = node[char]
14+
node[self.end] = True
15+
16+
def search(self, word: str) -> bool:
17+
node = self.root
18+
for char in word:
19+
if char not in node:
20+
return False
21+
node = node[char]
22+
return self.end in node
23+
24+
def startsWith(self, prefix: str) -> bool:
25+
node = self.root
26+
for char in prefix:
27+
if char not in node:
28+
return False
29+
node = node[char]
30+
return True
31+

word-break/HodaeSsi.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 시간복잡도 : O(n^2)
2+
# 공간복잡도 : O(n)
3+
class Solution:
4+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
5+
dp = [False] * (len(s) + 1)
6+
dp[0] = True
7+
8+
for i in range(1, len(s) + 1):
9+
for j in range(i):
10+
if dp[j] and s[j:i] in wordDict:
11+
dp[i] = True
12+
break
13+
14+
return dp[-1]
15+

0 commit comments

Comments
 (0)