Skip to content

121. Best Time to Buy and Sell Stock#35

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

121. Best Time to Buy and Sell Stock#35
seal-azarashi wants to merge 4 commits intomainfrom
best-time-to-buy-and-sell-stock

Conversation

@seal-azarashi
Copy link
Owner


## Step 3

i 日目の価格 - (i - 1) 日目までの最小の価格を計算して最大利益を出す方法が最も素直でエレガントに思えたので採用した。
Copy link

Choose a reason for hiding this comment

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

個人的には買いの日と売りの日をインデックスで指して更新する方法とあまり違うように感じませんでした。profit < 0 のときに buyDay = sellDay として動作するのは注目している日の価格が今までの中で最小だからですよね?インデックスを保存するかそれが指す値を保存するかという違いだけに見えます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@fhiyo
返信が大変遅くなりました。
確かに処理の内容はあまり違いが無いとは思います。個人的にはこちらの方が読みやすいし書きやすくていいなぐらいの気持ちで書いてました (エレガントは言い過ぎだったかもですね)

for (int sellDay = 1; sellDay < prices.length; sellDay++) {
int profit = prices[sellDay] - prices[buyDay];
if (profit < 0) {
buyDay = sellDay;

Choose a reason for hiding this comment

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

株価が下がり続ける場合、ループを抜けたとき本来のbuyDayとズレそうです

buyDayというより、株価が底を打ってる日なので、bottomDayとかでしょうか?

Choose a reason for hiding this comment

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

株価が下がり続ける場合、ループを抜けたとき本来のbuyDayとズレそうです

すいませんこれ正確じゃないですね。本来のbuyDayとずれるというより、買わないのにbuyDayが発生するということに違和感を覚えたんだと思います

Copy link
Owner Author

Choose a reason for hiding this comment

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

@Yoshiki-Iwasa
返信が大変おそくなりました。
確かに買わないかもしれないの日が buyDay に代入されていると変な感じですね。うーんしかし改善案が難しいですね... 違和感を払拭するには tentativeBuyDay とかにするのを思いつきますが、冗長に感じます。


### その他気になったコメントなど

- scanl という考え方: https://github.com/goto-untrapped/Arai60/pull/58/files#r1782742318
Copy link

Choose a reason for hiding this comment

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

一度、関数型的な頭の動かし方は覚えておくといいように思います。

return 0;
}

int maxProfit = 0;
Copy link

Choose a reason for hiding this comment

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

関数名と被っているのが気になりました。このmaxProfitminProfitSoFarと同様にその時点での最大値を表すので揃えるならばmaxPriceSoFarとするのが良いかもしれません。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@rihib
返信が大変おそくなりました。
確かにそうですね。あまり考えていませんでした。しかし関数の最後では so far でなく prices の要素全てを操作した値になるので、関数名と被っていることを除けばそんなに悪くない命名なのかなとも思います。
Leetcode 上では出来ないですが、むしろ関数名の方を変えたい気持ちもあります。

Copy link

Choose a reason for hiding this comment

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

それは同感ですw

Comment on lines +160 to +162
if (prices == null || prices.length == 0) {
return 0;
}
Copy link

@rihib rihib Oct 9, 2024

Choose a reason for hiding this comment

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

空がきたときに0を返すというのは条件として提示されたものではないので、ご自身で空がきたときは0を返すのが良いと判断したということを明記するのが良いと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@rihib
返信が大変おそくなりました。
そうですね。「問題文に指定はないが、買いや売りをする日がそもそもなければ利益は生み出しようがないので 0 を返すこととする」とコメントを残しました。

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.

5 participants