-
Notifications
You must be signed in to change notification settings - Fork 0
121. Best Time to Buy and Sell Stock #20
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ## 問題: [121. Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Step 1 | ||
|
|
||
| - 変数を以下の通りにする | ||
| - stockを買う日: `buy_day` | ||
| - その日の値段: `price[buy_day]` | ||
| - 最大利益: `max_profit` | ||
| - forループを回していく中で、`price[i]`が`price[buy_day]`より安い場合は`price[buy_day]`を`price[i]`に更新する。高い場合は`max_profit`を更新する | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| max_profit = 0 | ||
| buy_day = 0 | ||
|
|
||
| for i, price in enumerate(prices): | ||
| if price < prices[buy_day]: | ||
| buy_day = i | ||
|
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. このままでもほとんど問題ないと思いますが ,あえて挙げるなら買う"日" (追記) 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. 私も...dayという変数にインデックスを入れているのは少し分かりづらい気がしました。 |
||
| else: | ||
|
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. step2,3でminを使った実装に置き換えられていて良いと思いました。 |
||
| max_profit = max(max_profit, price - prices[buy_day]) | ||
|
|
||
| return max_profit | ||
| ``` | ||
|
|
||
| 時間計算量: O(n) | ||
|
|
||
| 空間計算量: O(1) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Step 2 | ||
|
|
||
| - ループ中にインデックスを気にせずとも書けた | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| max_profit = 0 | ||
| buying_price = prices[0] | ||
|
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. 問題の制約上そのような入力は Leet Code からは与えられませんが,開発現場でこのメソッドを実装するなら, def maxProfit(self, prices: List[int]) -> int:
if not prices:
return 0
max_profit = 0
buying_price = prices[0]
# (以下同じ) |
||
|
|
||
| for price in prices: | ||
| buying_price = min(buying_price, price) | ||
| max_profit = max(max_profit, price - buying_price) | ||
|
|
||
| return max_profit | ||
| ``` | ||
|
|
||
| 時間計算量: O(n) | ||
|
|
||
| 空間計算量: O(1) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Step 3 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| max_profit = 0 | ||
| buying_price = prices[0] | ||
|
|
||
| for price in prices: | ||
| buying_price = min(buying_price, price) | ||
| max_profit = max(max_profit, price - buying_price) | ||
|
Comment on lines
+10
to
+11
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. 個人的には、max_profit の計算を先にしたいと思いました。 |
||
|
|
||
| return max_profit | ||
| ``` | ||
|
|
||
| 1回目: 1分 9秒 | ||
|
|
||
| 2回目: 1分 10秒 | ||
|
|
||
| 3回目: 1分 11秒 | ||
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のなかのminを保持したいという意図が伝わりやすい気がしています。