-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #883 from HodaeSsi/main
[HodaeSsi] Week 5
- Loading branch information
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# 시간 복잡도 : O(n) | ||
# 공간 복잡도 : O(1) | ||
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
min_price = float('inf') | ||
max_profit = 0 | ||
|
||
for price in prices: | ||
min_price = min(min_price, price) | ||
max_profit = max(max_profit, price - min_price) | ||
|
||
return max_profit | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# 시간복잡도: O(n) | ||
# 공간복잡도: O(1) | ||
|
||
class Solution: | ||
""" | ||
@param: strs: a list of strings | ||
@return: encodes a list of strings to a single string. | ||
""" | ||
def encode(self, strs): | ||
if not strs: | ||
return "" | ||
return chr(257).join(strs) | ||
|
||
""" | ||
@param: str: A string | ||
@return: decodes a single string to a list of strings | ||
""" | ||
def decode(self, str): | ||
if not str: | ||
return [] | ||
return str.split(chr(257)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# 시간복잡도 : O(n*mlogm) (n은 strs의 길이, m은 strs의 원소의 길이) | ||
# 공간복잡도 : O(n) | ||
|
||
class Solution: | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
anagrams = collections.defaultdict(list) | ||
for word in strs: | ||
anagrams[''.join(sorted(word))].append(word) | ||
return list(anagrams.values()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# 시간복잡도: O(n) (n은 문자열의 길이) | ||
# 공간복잡도: O(n) (n은 문자열의 길이) | ||
class Trie: | ||
def __init__(self): | ||
self.root = {} | ||
self.end = False | ||
|
||
def insert(self, word: str) -> None: | ||
node = self.root | ||
for char in word: | ||
if char not in node: | ||
node[char] = {} | ||
node = node[char] | ||
node[self.end] = True | ||
|
||
def search(self, word: str) -> bool: | ||
node = self.root | ||
for char in word: | ||
if char not in node: | ||
return False | ||
node = node[char] | ||
return self.end in node | ||
|
||
def startsWith(self, prefix: str) -> bool: | ||
node = self.root | ||
for char in prefix: | ||
if char not in node: | ||
return False | ||
node = node[char] | ||
return True | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# 시간복잡도 : O(n^2) | ||
# 공간복잡도 : O(n) | ||
class Solution: | ||
def wordBreak(self, s: str, wordDict: List[str]) -> bool: | ||
dp = [False] * (len(s) + 1) | ||
dp[0] = True | ||
|
||
for i in range(1, len(s) + 1): | ||
for j in range(i): | ||
if dp[j] and s[j:i] in wordDict: | ||
dp[i] = True | ||
break | ||
|
||
return dp[-1] | ||
|