-
-
Notifications
You must be signed in to change notification settings - Fork 305
[ZetBe] WEEK 05 solutions #2173
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
5 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,23 @@ | ||
| ''' | ||
| 문제: 한번의 구매와 판매로 얻을 수 있는 최대 이익을 구하라. | ||
| 풀이: 주어진 가격 리스트를 순회하면서 최저가와 최고가를 갱신하며 최대 이익을 계산한다. | ||
| 최저가는 현재 최저가격보다 낮은 가격이 나오면 갱신하고, | ||
| 최고가는 현재 최고가격보다 높은 가격이 나오면 갱신및 정답인지 비교한다. | ||
| 시간복잡도: O(n) | ||
| 공간복잡도: O(1) | ||
|
|
||
| ''' | ||
|
|
||
|
|
||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| ma, mi = prices[0], prices[0] | ||
| answ = 0 | ||
| for i in range(1, len(prices)): | ||
| if mi > prices[i]: | ||
| mi, ma = prices[i], prices[i] | ||
| if ma < prices[i]: | ||
| ma = prices[i] | ||
| answ = max(answ, ma-mi) | ||
| return answ | ||
|
|
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 @@ | ||
| ''' | ||
| 문제: 주어진 문자열들을 아나그램끼리 그룹화하는 코드를 작성하시오. | ||
| 풀이: 각 문자열을 정렬하여 동일한 아나그램끼리 같은 키로 묶어 딕셔너리에 저장한 후, 그 값을 반환합니다.(이 때, 해당 문자열을 정렬함으로써 일종의 키 값을 만듭니다.) | ||
| 시간복잡도: O(n * k log k) (n은 문자열의 개수, k는 각 문자열의 최대 길이) | ||
| 공간복잡도: O(n) | ||
|
|
||
| ''' | ||
|
|
||
|
|
||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| d = {} | ||
|
|
||
| for i in strs: | ||
| k = ''.join(sorted(i)) | ||
| if k not in d: | ||
| d[k] = [] | ||
| d[k].append(i) | ||
| return list(d.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,34 @@ | ||
| ''' | ||
| 문제: 특정 명령이 주어졌을 때, 트라이(Trie) 자료구조를 구현하는 코드를 작성하시오. | ||
| 풀이: 단순히 문자열을 저장하는 리스트를 사용하여 트라이 자료구조의 기능을 구현합니다. | ||
| ''' | ||
|
|
||
|
|
||
| class Trie: | ||
|
|
||
| def __init__(self): | ||
| self.arr = [] | ||
|
|
||
| def insert(self, word: str) -> None: | ||
| self.arr.append(word) | ||
|
|
||
| def search(self, word: str) -> bool: | ||
| for i in self.arr: | ||
| if i == word: | ||
| return True | ||
| return False | ||
|
|
||
| def startsWith(self, prefix: str) -> bool: | ||
| n = len(prefix) | ||
| for i in self.arr: | ||
| if i[:n] == 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,23 @@ | ||
| ''' | ||
| 문제: 주어진 문자열이 사전에 있는 단어들로 구성될 수 있는지 여부를 판단하는 코드를 작성하시오. | ||
| 풀이: 동적 프로그래밍(DP)을 사용하여 문자열의 각 위치까지 사전에 있는 단어들로 구성될 수 있는지 여부를 기록합니다. | ||
| 시간복잡도: O(n*m) (n은 문자열 s의 길이, m은 단어 사전의 단어 수) | ||
| 공간복잡도: O(n) | ||
| ''' | ||
|
|
||
|
|
||
| class Solution: | ||
| def wordBreak(self, s: str, wordDict: List[str]) -> bool: | ||
| dp = [False for i in range(len(s))] | ||
| for i in wordDict: | ||
|
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. 해당 부분을 해결하지 못해서 힌트를 보고 풀었는데 이렇게 빼내서 해결할 수도 있었겠네요 |
||
| if i == s[:len(i)]: | ||
| dp[len(i)-1] = True | ||
|
|
||
| for i in range(1, len(s)): | ||
| if dp[i-1] == True: | ||
|
|
||
| for j in wordDict: | ||
| if i+len(j) <= len(s) and j == s[i:i+len(j)]: | ||
| dp[i+len(j)-1] = True | ||
| return dp[len(s)-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.
자료구조만 변경해도 search 시 효율이 더 나오지 않을까 하는 생각입니다!