[yyyyyyyyyKim] WEEK 05 solutions#1386
Merged
yyyyyyyyyKim merged 5 commits intoDaleStudy:mainfrom May 3, 2025
Merged
Conversation
seungriyou
approved these changes
May 2, 2025
| buy = prices[i] | ||
|
|
||
| if prices[i] - buy > profit: | ||
| profit = prices[i] - buy |
Contributor
There was a problem hiding this comment.
buy의 경우에는 buy = min(buy, prices[i])으로, profit의 경우에는 profit = max(profit, prices[i] - buy)로 업데이트한다면 if 문을 삭제해 코드 라인 수를 더 줄일 수 있을 것 같아요!
또한 for loop 내에서 인덱스 i를 사용하지 않고 prices[i]만 사용하기 때문에, for price in prices로 순회할 수도 있을 것 같습니다 😄
|
|
||
| # answer에 키가 없으면 키 추가 | ||
| if key not in answer: | ||
| answer[key] = [] |
Contributor
There was a problem hiding this comment.
이렇게 하면 defaultdict를 사용하지 않고도 없는 key 처리를 깔끔하게 할 수가 있군요! 배워갑니다 🤩
| for i in range(1,len(s)+1): | ||
| # j : 단어 자르는 시작위치. s[:j]가 wordDict에 있고, s[j:i]가 wordDict에 있는지 확인. | ||
| for j in range(i): | ||
| if dp[j] and s[j:i] in wordDict: |
Contributor
There was a problem hiding this comment.
저도 처음에 yyyyyyyyyKim님과 유사하게 풀이했으나 솔루션을 참고하고 나서 두 가지 관점에서 시간 복잡도를 더 최적화할 수 있다는 걸 알게되었는데요, 혹시나 도움이 되실까하여 공유드립니다~!
-
str in wordDict:in으로 조회하는 시간 복잡도를 O(n) ➡️ O(1) 최적화wordDict는 list이기 때문에str을in으로 찾을 때 순차적으로 찾게되어 시간 복잡도가 O(n)이 됩니다.wordDict에서str을in으로 찾을 때 O(1)에 찾으려면wordDict를 set으로 만들면 됩니다.
-
s[j:i] in wordDict:s[j:i]문자열 슬라이싱을 trie로 대체하여 시간 복잡도를 O(n) ➡️ O(1) 최적화str in wordDict를 O(1)으로 줄였어도,str을s[j:i]와 같이 문자열 슬라이싱으로 만들면 O(n)이 소요되게 됩니다.- Implement Trie Prefix Tree #256 문제에서 사용하셨던 trie 자료구조를 이용하면, trie를 타고 내려가면서 매 루프마다 가장 끝 문자만 확인하면 되기 때문에 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
답안 제출 문제
작성자 체크 리스트
In Review로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!