-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hu6r1s] WEEK 05 Solutions #1840
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
+119
−0
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fc48533
feat: Solve best-time-to-buy-and-sell-stock problem
hu6r1s d81cc20
feat: Solve group-anagrams problem
hu6r1s 93d393f
feat: Solve encode-and-decode-strings problem
hu6r1s 2e423e4
feat: Solve word-break problem
hu6r1s 12d1fc0
feat: Solve implement-trie-prefix-tree problem
hu6r1s 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,25 @@ | ||
class Solution: | ||
""" | ||
1. 브루트포스 | ||
2중 for문이라서 O(n^2)임. | ||
prices의 길이가 10^5이므로 10^10이 되면서 시간초과가 발생 | ||
2. 이분탐색으로 가능할까 했지만 DP를 사용해야 하는 문제 같음 | ||
시간복잡도는 O(n)이 나옴 | ||
""" | ||
""" | ||
def maxProfit(self, prices: List[int]) -> int: | ||
max_profit = 0 | ||
for i in range(len(prices)-1): | ||
for j in range(i, len(prices)): | ||
profit = prices[j] - prices[i] | ||
max_profit = max(max_profit, profit) | ||
return max_profit | ||
""" | ||
def maxProfit(self, prices: List[int]) -> int: | ||
max_profit = 0 | ||
min_price = prices[0] | ||
for price in prices: | ||
max_profit = max(max_profit, price - min_price) | ||
min_price = min(price, min_price) | ||
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,14 @@ | ||
class Solution: | ||
""" | ||
@param: strs: a list of strings | ||
@return: encodes a list of strings to a single string. | ||
""" | ||
def encode(self, strs): | ||
return "secretKey!@#".join(strs) | ||
|
||
""" | ||
@param: str: A string | ||
@return: decodes a single string to a list of strings | ||
""" | ||
def decode(self, str): | ||
return str.split("secretKey!@#") |
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 @@ | ||
from collections import defaultdict | ||
|
||
class Solution: | ||
""" | ||
1. 정렬된 값이 딕셔너리에 있으면 리스트 형식으로 삽입 | ||
""" | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
dict_strs = defaultdict(list) | ||
|
||
for word in strs: | ||
dict_strs[str(sorted(word))].append(word) | ||
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. str(sorted(word)) 대신 tuple(sorted(word))가 더 효율적 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. 좋은 정보 감사합니다! |
||
return list(dict_strs.values()) |
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.
prices가 빈 배열일 경우 prices[0] 접근에서 IndexError 발생 가능 → 입력 검증 필요
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.
배열의 길이는 1이상이라서 빈 배열이 나올 경우가 없어 검증이 필요없을 것 같습니다.
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.
아 제가 조건을 못 봤네요. 이 문제에서는 해당 사항 고려하지 않으셔도 됩니다.