-
-
Notifications
You must be signed in to change notification settings - Fork 245
[KwonNayeon] Week 5 #853
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
[KwonNayeon] Week 5 #853
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
091227d
chore: add placeholder for "Best time to buy and sell stock"
KwonNayeon 6ca4a28
Solved "Best Time to Buy and Sell Stock"
KwonNayeon 8154a14
Solved "Group Anagrams"
KwonNayeon d5e43c3
chore: Fix code formatting
KwonNayeon ab67504
chore: Fix code formatting
KwonNayeon 22af2e7
chore: Edit file name
KwonNayeon b92c6ba
Solved "Encode and decode strings"
KwonNayeon 585e7fd
Add understanding of Trie implementation problem
KwonNayeon e8a5387
Add study notes for Trie implementation
KwonNayeon 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,29 @@ | ||
""" | ||
Constraints: | ||
1. 1 <= prices.length <= 10^5 | ||
2. 0 <= prices[i] <= 10^4 | ||
|
||
Time Complexity: O(n) | ||
|
||
Space Complexity: O(1) | ||
|
||
풀이 방법: | ||
- 배열을 한 번 순회하면서: | ||
1. 현재까지의 최소 가격(min_price)을 계속 갱신 | ||
2. 현재 가격에서 최소 가격을 뺀 값(현재 가능한 이익)과 기존 최대 이익을 비교하여 더 큰 값을 저장 | ||
- 이 방식으로 각 시점에서 가능한 최대 이익을 계산함 | ||
|
||
To Do: | ||
- 다른 접근 방법 찾아보기 (Two Pointers, Dynamic Programming) | ||
""" | ||
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
min_price = prices[0] | ||
max_profit = 0 | ||
|
||
for i in range(1, len(prices)): | ||
min_price = min(min_price, prices[i]) | ||
max_profit = max(max_profit, prices[i] - min_price) | ||
|
||
return max_profit | ||
|
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.
다른 접근 방법(Two Pointers, Dynamic Programming)을 고려해 보라고 주석에 적혀 있는데, 이 코드와 비교했을 때 어떤 장단점이 있을까요?
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.
@lledellebell 제약조건이 추가되었을 때를 고려해볼 수 있을 것 같습니다.
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.
@EgonD3V 조금 늦었지만, 답변 감사합니다!