Skip to content

122. Best Time to Buy and Sell Stock II#36

Open
seal-azarashi wants to merge 2 commits intomainfrom
best-time-to-buy-and-sell-stock-ii
Open

122. Best Time to Buy and Sell Stock II#36
seal-azarashi wants to merge 2 commits intomainfrom
best-time-to-buy-and-sell-stock-ii

Conversation

@seal-azarashi
Copy link
Owner

seal_azarashi added 2 commits October 8, 2024 07:25

int maxProfit = 0;
for (int i = 0; i < prices.length - 1; i++) {
int profit = prices[i + 1] - prices[i];
Copy link

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
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);


## Step 2

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

Choose a reason for hiding this comment

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

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

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

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)とかがいいんでしょうか。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants