-
Notifications
You must be signed in to change notification settings - Fork 0
122. Best Time to Buy and Sell Stock II #36
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,87 @@ | ||
| # 122. Best Time to Buy and Sell Stock II | ||
|
|
||
| LeetCode URL: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ | ||
|
|
||
| この問題は Java で解いています。 | ||
| 各解法において、メソッドが属するクラスとして `Solution` を定義していますが、これは Java の言語仕様に従い、コードを実行可能にするために必要なものです。このクラス自体には特定の意味はなく、単にメソッドを組織化し、実行可能にするためのものです。 | ||
|
|
||
| ## Step 1 | ||
|
|
||
| いつ買っても売ってもいいし、期間内の値動きが全て分かってるなら、明日値が上がるなら買っておいて次の日売る、下がるなら買わない、を繰り返せば最大利益が出せるなと考え、そのように実装した。 | ||
| (💭 現実でもこれが出来たら良いのに) | ||
|
|
||
| ```java | ||
| /** | ||
| * 解いた時間: 不明 (解法はご飯食べながら考えてて思いつきました) | ||
| * 時間計算量: O(n): 配列の全ての要素を走査し、利益が出せる場合に最大利益に加算していく | ||
| * 空間計算量: O(1): 最大利益とイテレーションごとに算出した利益を格納する変数 | ||
| */ | ||
| class Solution { | ||
| public int maxProfit(int[] prices) { | ||
| if (prices == null) { | ||
| return 0; | ||
| } | ||
|
|
||
| int maxProfit = 0; | ||
| for (int i = 0; i < prices.length - 1; i++) { | ||
| int profit = prices[i + 1] - prices[i]; | ||
| if (profit > 0) { | ||
| maxProfit += profit; | ||
| } | ||
| } | ||
| return maxProfit; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Step 2 | ||
|
|
||
| 株を保有している、していない状態それぞれの最大利益を算出していく方法。自分では思いつかなかった。 | ||
|
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. holdingStockのためには株を買わなくてはならないので必ずマイナススタートになるのは良いのですが、これがprofitなのかというとなんか引っかかるので、PL(Profit & Loss)とかがいいんでしょうか。 |
||
|
|
||
| ```java | ||
| /** | ||
| * 時間計算量: O(n): 配列の全ての要素を走査する | ||
| * 空間計算量: O(1): 株保有、非保有時それぞれの最大利益を保持する変数 | ||
| */ | ||
| class Solution { | ||
| public int maxProfit(int[] prices) { | ||
| if (prices == null || prices.length == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| int holdingStockProfit = -prices[0]; | ||
| int withoutStockProfit = 0; | ||
| for (int i = 1; i < prices.length; i++) { | ||
| holdingStockProfit = Math.max(holdingStockProfit, withoutStockProfit - prices[i]); | ||
| withoutStockProfit = Math.max(withoutStockProfit, holdingStockProfit + prices[i]); | ||
| } | ||
| return withoutStockProfit; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Step 3 | ||
|
|
||
| ```java | ||
| /** | ||
| * 解いた時間: 約2分 | ||
| * 時間計算量: O(n): 配列の全ての要素を走査し、利益が出せる場合に最大利益に加算していく | ||
| * 空間計算量: O(1): 最大利益とイテレーションごとに算出した利益を格納する変数 | ||
| */ | ||
| class Solution { | ||
| public int maxProfit(int[] prices) { | ||
| if (prices == null) { | ||
| return 0; | ||
| } | ||
|
|
||
| int maxProfit = 0; | ||
| for (int i = 0; i < prices.length - 1; i++) { | ||
| int profit = prices[i + 1] - prices[i]; | ||
| if (profit > 0) { | ||
| maxProfit += profit; | ||
| } | ||
| } | ||
| return maxProfit; | ||
| } | ||
| } | ||
| ``` | ||
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.
maxprofit += Math.max(0, prices[i + 1] - prices[i]);とも書けるのですが、かえって分かりにくくなっているかもしれません。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.
@nodchip
返信が大変おそくなりました。
そうですね、
prices[i + 1] - prices[i]が profit であるというのは変数にするなりして示しておいてほしい気持ちはあります。行数は増えますがこのような形で: