-
Notifications
You must be signed in to change notification settings - Fork 0
Create 121. Best Time to Buy and Sell Stock.md #37
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
Open
tokuhirat
wants to merge
1
commit into
main
Choose a base branch
from
121.-Best-Time-to-Buy-and-Sell-Stock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
49 changes: 49 additions & 0 deletions
49
121. Best Time to Buy and Sell Stock/121. Best Time to Buy and Sell Stock.md
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,49 @@ | ||
| # 121. Best Time to Buy and Sell Stock | ||
| ## STEP1 | ||
| - 何も見ずに解いてみる | ||
| - これまでの最小値と現在の価格の差の最大値を求めればよい。 | ||
| ```python | ||
| import math | ||
|
|
||
|
|
||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| min_prices_so_far = math.inf | ||
| max_profit = 0 | ||
| for price in prices: | ||
| max_profit = max(max_profit, price - min_prices_so_far) | ||
| min_prices_so_far = min(min_prices_so_far, price) | ||
| return max_profit | ||
| ``` | ||
| #### memo | ||
| 時間計算量: $O(n)$ | ||
| 空間計算量: $O(1)$ | ||
|
|
||
| ## STEP2 | ||
| ### プルリクやドキュメントを参照 | ||
| - https://discord.com/channels/1084280443945353267/1206101582861697046/1219181674038820945 | ||
| > prices が空の場合 | ||
| - 0 となる。微妙な気はするが利益を出せない時は 0 という設計の関数なのでOKということにした。 | ||
| - min_prices_so_far = math.inf とするか prices[0] とするか。0番目を特別扱いしない方が好み。 | ||
| - https://github.com/olsen-blue/Arai60/pull/37/files | ||
| - 配列でそれぞれの日に得られる最大利益を記録。それほどわかりやすくならないかなと思った。 | ||
| - House Robber の場合、前の家とその前の家の情報を使うため index で情報を取り出すことで漸化式のように理解でき見通しが良くなる。今回の問題では、max_profit[i] = prices[i] - min(prices[:i]) であり、min の部分を変数で更新しつつ管理する都合で、あまりその利益を得られないから。 | ||
| - https://github.com/hayashi-ay/leetcode/pull/52/files | ||
| - STEP1 と同じ解法。 | ||
| ## STEP3 | ||
| ### 3回ミスなく書く | ||
| ```python | ||
| import math | ||
|
|
||
|
|
||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| min_price_so_far = math.inf | ||
| max_profit = 0 | ||
| for price in prices: | ||
| max_profit = max(max_profit, price - min_price_so_far) | ||
| min_price_so_far = min(min_price_so_far, price) | ||
| return max_profit | ||
| ``` | ||
|
|
||
| 1分で1回Accept (STEP1と同じため省略) | ||
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.
_so_farは個人的にはつけないです。やや冗長に感じました。
また、max_profitも変数の意味合いとしては同じに感じますが、min_priceのみの理由が気になりました。
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.
ありがとうございます。_so_far の有無を厳密に使い分けておりませんでした。max_profit は最後に確定した値にフォーカスしていたので _so_far をつけなかった程度です。min_price についても確かに不要かもしれません。
以下のコメントも見つけました。
quinn-sasha/leetcode#24 (comment)