-
-
Notifications
You must be signed in to change notification settings - Fork 305
[doh6077] WEEK 05 Solutions #2164
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
8 commits
Select commit
Hold shift + click to select a range
3f75a5b
121. Best Time to Buy and Sell Stock Solution
doh6077 82c3b57
49. Group Anagrams Solution
doh6077 05699c5
Merge branch 'main' of https://github.com/doh6077/leetcode-study
doh6077 386746f
줄바꿈
doh6077 cea3eef
.
doh6077 90db269
139. Word Break Solution
doh6077 bc00ba9
208. Implement Trie (Prefix Tree) Solution
doh6077 4970077
271. Encode and Decode Strings Solution
doh6077 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,18 @@ | ||
|
|
||
| # 121. Best Time to Buy and Sell Stock | ||
| # O(n) - Two Pointers | ||
| class Solution: | ||
| def maxProfit(self, prices: list[int]) -> int: | ||
| left = 0 | ||
| right = 1 | ||
| profit = 0 | ||
| max_profit = 0 | ||
|
|
||
| while right < len(prices): | ||
| if prices[right] > prices[left]: | ||
| profit = prices[right] - prices[left] | ||
| max_profit = max(profit, max_profit) | ||
| else: | ||
| left = right | ||
| right += 1 | ||
| return max_profit |
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,32 @@ | ||
|
|
||
| # 208. Implement Trie (Prefix Tree) | ||
| class Trie: | ||
|
|
||
| def __init__(self): | ||
| self.list = [] | ||
|
|
||
|
|
||
| def insert(self, word: str) -> None: | ||
| self.list.append(word) | ||
|
|
||
|
|
||
| def search(self, word: str) -> bool: | ||
| if word in self.list: | ||
| return True | ||
| else: | ||
| return False | ||
|
|
||
|
|
||
| def startsWith(self, prefix: str) -> bool: | ||
| for i, value in enumerate(self.list): | ||
| if value.startswith(prefix): | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
|
|
||
| # 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,26 @@ | ||
|
|
||
|
|
||
| # 271. Encode and Decode Strings | ||
|
|
||
| # integer represents the following word's length | ||
| # read until we reach to delimiter | ||
|
|
||
| class Solution: | ||
| def encode(self, strs): | ||
| res = "" | ||
| for s in strs: | ||
| res += str(len(s)) + "#" + s | ||
| return res | ||
|
|
||
| def decode(self, str): | ||
| res, i = [], 0 | ||
|
|
||
| while i < len(str): | ||
| j = i | ||
| while str[j] != "#": | ||
| j += 1 | ||
| length = int(str[i:j]) | ||
| res.append(str[j + 1 : j + 1 + length]) | ||
| i = j + 1 + length | ||
|
|
||
| return res |
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,16 @@ | ||
| from collections import defaultdict | ||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| # hashmap | ||
| # calculate frequency | ||
| # 26 alphabet characters | ||
| anagrams_dict = defaultdict(list) | ||
| for s in strs: | ||
| count = [0] * 26 | ||
| for c in s: | ||
| count[ord(c) - ord('a')] += 1 | ||
|
|
||
| key = tuple(count) | ||
| anagrams_dict[key].append(s) | ||
|
|
||
| return list(anagrams_dict.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,12 @@ | ||
| class Solution: | ||
| def wordBreak(self, s: str, wordDict: List[str]) -> bool: | ||
| dp = [True] + [False] * len(s) | ||
|
|
||
| for i in range(1, len(s) + 1): | ||
| for w in wordDict: | ||
| start = i - len(w) | ||
| if start >= 0 and dp[start] and s[start:i] == w: | ||
| dp[i] = True | ||
| break | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 문제를 LIS dp 문제와 유사하게 풀 수 있었군요. 좋은 경험이었습니다. |
||
|
|
||
| return dp[-1] | ||
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.
이거 파일을 다른 폴더에 넣으신 것 같습니다!
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.
그리고, 통과는 되었지만, Trie 구조와는 거리가 먼 것으로 보여서, 시간이 되시면, 한 번 만들어 보시는 것도 좋을 것 같습니다!