-
-
Notifications
You must be signed in to change notification settings - Fork 339
[gcount85] WEEK 05 Solutions #2500
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
+118
−0
Merged
Changes from all commits
Commits
Show all changes
4 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,21 @@ | ||
| """ | ||
| # Approach | ||
| 지금까지의 최저 가격을 갱신함과 동시에 최선의 이익도 업데이트합니다. | ||
|
|
||
| # Complexity | ||
| - Time complexity: O(N) | ||
|
|
||
| - Space complexity: O(1) | ||
| """ | ||
|
|
||
|
|
||
| class Solution: | ||
| def maxProfit(self, prices: list[int]) -> int: | ||
| min_price = float("inf") | ||
| answer = 0 | ||
|
|
||
| for price in prices: | ||
| min_price = min(min_price, price) | ||
| answer = max(answer, price - min_price) | ||
|
|
||
| return answer |
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,21 @@ | ||
| """ | ||
| # Approach | ||
| strs 배열을 순회하며 문자열을 정규화(정렬)하고, | ||
| 정규화 값이 같은 원소들끼리 모이도록 딕셔너리에 추가하여 최종 값을 반환한다. | ||
|
|
||
| # Complexity | ||
| strs의 길이를 N, 문자열의 길이를 K라고 할 때, | ||
|
|
||
| - Time complexity: O(N*KlogK) | ||
| - Space complexity: O(N*K) | ||
| """ | ||
|
|
||
| from collections import defaultdict | ||
|
|
||
|
|
||
| class Solution: | ||
| def groupAnagrams(self, strs: list[str]) -> list[list[str]]: | ||
| anagram = defaultdict(list) # normalized str : str list | ||
| for s in strs: | ||
| anagram["".join(sorted(s))].append(s) | ||
| return list(anagram.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,41 @@ | ||
| class TrieNode: | ||
| def __init__(self): | ||
| self.children = {} | ||
| self.is_end = False | ||
|
|
||
|
|
||
| class Trie: | ||
|
|
||
| def __init__(self): | ||
| self.root = TrieNode() | ||
|
|
||
| def insert(self, word: str) -> None: | ||
| node = self.root | ||
| for ch in word: | ||
| if ch not in node.children: | ||
| node.children[ch] = TrieNode() | ||
| node = node.children[ch] | ||
| node.is_end = True | ||
|
|
||
| def search(self, word: str) -> bool: | ||
| node = self.root | ||
| for ch in word: | ||
| if ch not in node.children: | ||
| return False | ||
| node = node.children[ch] | ||
| return node.is_end | ||
|
|
||
| def startsWith(self, prefix: str) -> bool: | ||
| node = self.root | ||
| for ch in prefix: | ||
| if ch not in node.children: | ||
| return False | ||
| node = node.children[ch] | ||
| 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,35 @@ | ||
| """ | ||
| # Intuition | ||
| wordDict를 word들의 길이로 분류하고, | ||
| s의 각 위치까지의 문자열을 완성할 수 있는지 dp 배열로 확인합니다. | ||
|
|
||
| # Complexity | ||
| wordDict의 길이를 N, s의 길이를 K | ||
| - Time complexity: O(N+K) | ||
| - Space complexity: O(N+K) | ||
| """ | ||
|
|
||
| from collections import defaultdict | ||
|
|
||
|
|
||
| class Solution: | ||
| def wordBreak(self, s: str, wordDict: list[str]) -> bool: | ||
| n = len(s) | ||
| dp = [False] * (n + 1) | ||
| word_dict = defaultdict(set) | ||
| for word in wordDict: | ||
| word_dict[len(word)].add(word) | ||
|
|
||
| dp[0] = True | ||
| for i in range(1, n + 1): | ||
| for ( | ||
| k, | ||
| v, | ||
| ) in word_dict.items(): | ||
| if i + k - 1 > n: | ||
| continue | ||
| if dp[i - 1] == False: | ||
| continue | ||
| if s[i - 1 : i + k - 1] in v: | ||
| dp[i + k - 1] = True | ||
| return dp[n] | ||
Oops, something went wrong.
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.
오.. 이 문제를 dp로 접근하신 것에 한 번 더 배우고 갑니다!