-
-
Notifications
You must be signed in to change notification settings - Fork 335
[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
Changes from 5 commits
7a1d0fb
8f7a907
e9eb1c4
4cddfb0
2f50432
4d39357
466f3be
00dd7c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| 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 i, price in enumerate(prices): | ||
| if price < buy: | ||
|
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. 현재 O(n^2) 으로 푸신것 같습니다! O(n)으로 풀 수 있는 방법도 확인해보시면 좋으실 듯 합니다!
Contributor
Author
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. 말씀해 주신 내용 반영해서 O(n)으로 개선해서 다시 풀어봤어요! 피드백 감사합니다 😊 |
||
| buy = price | ||
|
|
||
| for j in range(i + 1, len(prices)): | ||
| if prices[j] <= buy: | ||
| continue | ||
|
|
||
| if prices[j] - buy > profit: | ||
| profit = prices[j] - buy | ||
|
|
||
| return profit | ||
|
|
||
| # Time Complexity: O(n^2) | ||
| # Space Complexity: O(1) | ||
| 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. 🏷️ 알고리즘 패턴 분석
|
| 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) | ||
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.
🏷️ 알고리즘 패턴 분석