-
-
Notifications
You must be signed in to change notification settings - Fork 246
[hoyeongkwak] Week 5 Solutions #1851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,29 @@ | ||
''' | ||
time complexity : O(n) | ||
space complexity : O(2n) | ||
|
||
Algorithm : dp | ||
주식 보유하는 경우: | ||
- 이전에 이미 보유 : hold[i - 1] | ||
- i번째 날 새로 사는 경우 : -prices[i] | ||
|
||
주식을 팔았을 떄: | ||
- 이전에 이미 팜 : sold[i - 1] | ||
- i번째 날 파는 경우 : hold[i-1] + prices[i] | ||
''' | ||
|
||
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
if not prices or len(prices) < 2: | ||
return 0 | ||
n = len(prices) | ||
hold = [0] * n | ||
sold = [0] * n | ||
|
||
hold[0] = -prices[0] | ||
sold[0] = 0 | ||
|
||
for i in range(1, n): | ||
hold[i] = max(hold[i - 1], -prices[i]) | ||
sold[i] = max(sold[i - 1], hold[i - 1] + prices[i]) | ||
return sold[n - 1] |
This file contains hidden or 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,20 @@ | ||
''' | ||
m : 문자열 길이 | ||
n : 반복 | ||
time complexity : O(m * n * log m) | ||
space complexity : O(m * n) | ||
algorithm : sort, hash, | ||
각 문자열에 대해서 정렬을 하고, 정렬을 했을 때 동일한 문자에 대해서 hash table에 정렬 전 문자열을 추가 | ||
그리고 마지막에 list 형태로 hash table의 value에 대해서 돌려 준다 | ||
nat -> ant | ||
tan -> ant | ||
''' | ||
class Solution: | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
strResult = {} | ||
for word in strs: | ||
sortedStr = ''.join(sorted(word)) | ||
if sortedStr not in strResult: | ||
strResult[sortedStr] = [] | ||
strResult[sortedStr].append(word) | ||
return list(strResult.values()) |
This file contains hidden or 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,53 @@ | ||
''' | ||
insert | ||
time complexity : O(m) | ||
space complexity : O(m) | ||
|
||
search | ||
time complexity : O(m) | ||
space complexity : O(1) | ||
|
||
startWith | ||
time complexity : O(m) | ||
space complexity : O(1) | ||
''' | ||
|
||
class TrieNode: | ||
def __init__(self): | ||
self.children = {} | ||
self.isEnd = False | ||
|
||
class Trie: | ||
def __init__(self): | ||
self.root = TrieNode() | ||
|
||
def insert(self, word: str) -> None: | ||
node = self.root | ||
for char in word: | ||
if char not in node.children: | ||
node.children[char] = TrieNode() | ||
node = node.children[char] | ||
node.isEnd = True | ||
|
||
|
||
def search(self, word: str) -> bool: | ||
node = self.root | ||
for char in word: | ||
if char not in node.children: | ||
return False | ||
node = node.children[char] | ||
return node.isEnd | ||
|
||
def startsWith(self, prefix: str) -> bool: | ||
node = self.root | ||
for char in prefix: | ||
if char not in node.children: | ||
return False | ||
node = node.children[char] | ||
return True | ||
|
||
# Your Trie object will be instantiated and called as such: | ||
# obj = Trie() | ||
# obj.insert(word) | ||
# param_2 = obj.search(word) | ||
# param_3 = obj.startsWith(prefix) |
This file contains hidden or 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,56 @@ | ||
''' | ||
TrieNode | ||
a : alphabet size | ||
time complexity : O(m) | ||
space complexity : O(m * a) | ||
|
||
dp | ||
time complexity : O(n^2) | ||
space complexity : O(n) | ||
''' | ||
class TrieNode: | ||
def __init__(self): | ||
self.children = {} | ||
self.isEnd = False | ||
class Trie: | ||
def __init__(self): | ||
self.root = TrieNode() | ||
|
||
def insert(self, word): | ||
node = self.root | ||
for char in word: | ||
if char not in node.children: | ||
node.children[char] = TrieNode() | ||
node = node.children[char] | ||
node.isEnd = True | ||
|
||
def search(self, s, start_idx): | ||
node = self.root | ||
endPosition = [] | ||
|
||
for i in range(start_idx, len(s)): | ||
char = s[i] | ||
if char not in node.children: | ||
break | ||
node = node.children[char] | ||
if node.isEnd: | ||
endPosition.append(i + 1) | ||
return endPosition | ||
|
||
class Solution: | ||
def wordBreak(self, s: str, wordDict: List[str]) -> bool: | ||
trie = Trie() | ||
for word in wordDict: | ||
trie.insert(word) | ||
n = len(s) | ||
dp = [False] * (n + 1) | ||
dp[0] = True | ||
|
||
for i in range(n): | ||
if not dp[i]: | ||
continue | ||
endPositions = trie.search(s, i) | ||
|
||
for endPos in endPositions: | ||
dp[endPos] = True | ||
return dp[n] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
불필요하게 O(n) 공간을 사용