Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions arai60/Dynamic_Programming/best-time-to-buy-and-sell-stock-ii.md
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];
Copy link
Copy Markdown

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]); とも書けるのですが、かえって分かりにくくなっているかもしれません。

Copy link
Copy Markdown
Owner Author

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 であるというのは変数にするなりして示しておいてほしい気持ちはあります。行数は増えますがこのような形で:

            int profit = prices[i + 1] - prices[i];
            maxProfit += Math.max(0, profit);

if (profit > 0) {
maxProfit += profit;
}
}
return maxProfit;
}
}
```

## Step 2

株を保有している、していない状態それぞれの最大利益を算出していく方法。自分では思いつかなかった。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分もこれは思いつかなかったです。
holdingStockProfitが負の値からスタートするのが直感的ではなく

もっと前から株の売買をしていて、問題設定の日からスタートと考えれば自然なんですが

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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;
}
}
```