-
-
Notifications
You must be signed in to change notification settings - Fork 340
[jylee2033] WEEK 05 solutions #2503
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
+99
−0
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7a1d0fb
best time to buy and sell stock solution
jylee2033 8f7a907
Merge branch 'DaleStudy:main' into main
jylee2033 e9eb1c4
group anagrams solution
jylee2033 4cddfb0
Merge branch 'DaleStudy:main' into main
jylee2033 2f50432
encode and decode strings solution
jylee2033 4d39357
update maxProfit
jylee2033 466f3be
update maxProfit
jylee2033 00dd7c7
update maxProfit
jylee2033 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,27 @@ | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| # When length is 1, no profit possible | ||
| if len(prices) == 1: | ||
| return 0 | ||
|
|
||
| buy = 10 ** 5 | ||
| profit = 0 | ||
|
|
||
| # Iterate through prices | ||
| for price in prices: | ||
| # if price < buy: | ||
| # buy = price | ||
|
|
||
| # for j in range(i + 1, len(prices)): | ||
| # if prices[j] <= buy: | ||
| # continue | ||
|
|
||
| # if prices[j] - buy > profit: | ||
| # profit = prices[j] - buy | ||
| buy = min(buy, price) | ||
| profit = max(profit, price - buy) | ||
|
|
||
| return profit | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(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,58 @@ | ||
| class Solution: | ||
| """ | ||
| @param: strs: a list of strings | ||
| @return: encodes a list of strings to a single string. | ||
| """ | ||
| def encode(self, strs): | ||
| # write your code here | ||
| # Encode each word with its length prefix and a "#" | ||
| # ["C#", "&"] -> "2#C#1#&" | ||
| encoded_str = "" | ||
|
|
||
| for word in strs: | ||
| encoded_str += f"{len(word)}#{word}" | ||
|
|
||
| return encoded_str | ||
|
|
||
| """ | ||
| @param: str: A string | ||
| @return: decodes a single string to a list of strings | ||
| """ | ||
| def decode(self, str): | ||
| # write your code here | ||
| # "2#C#1#&" -> ["C#", "&"] | ||
| decoded_lst = [] | ||
| char_count = 0 | ||
| reading_word = False | ||
| word = "" | ||
| length_str = "" | ||
|
|
||
| if str == "": | ||
| return [""] | ||
|
|
||
| for ch in str: | ||
| if ch == "#" and not reading_word: | ||
| # Finished reading the length prefix | ||
| # Switch to word-reading mode | ||
| char_count = int(length_str) | ||
| length_str = "" | ||
| reading_word = True | ||
|
|
||
| elif not reading_word: | ||
| # Accumulate digits for the length prefix | ||
| length_str += ch | ||
|
|
||
| else: | ||
| # reading_word is True | ||
| word += ch | ||
| char_count -= 1 | ||
|
|
||
| if char_count == 0: | ||
| reading_word = False | ||
| decoded_lst.append(word) | ||
| word = "" | ||
|
|
||
| return decoded_lst | ||
|
|
||
| # Time Complexity: O(N) | ||
| # Space Complexity: O(N) |
|
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. 🏷️ 알고리즘 패턴 분석
|
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,14 @@ | ||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| anagram_map = {} | ||
| for word in strs: | ||
| key = "".join(sorted(word)) | ||
|
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. sorted한 키에 대해 단어를 추가하는 로직으로 깔끔하게 잘 짜신 것 같습니다! |
||
| if key not in anagram_map: | ||
| anagram_map[key] = [word] | ||
| else: | ||
| anagram_map[key].append(word) | ||
|
|
||
| return list(anagram_map.values()) | ||
|
|
||
| # Time Complexity: O(N * K log K), N - number of strings, K - maximum length of a string (for sorting) | ||
| # Space Complexity: O(N * K) | ||
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.
🏷️ 알고리즘 패턴 분석