Skip to content

Conversation

hroc135
Copy link
Owner

@hroc135 hroc135 commented Jan 13, 2025

Comment on lines +71 to +72
profitWithStock = max(profitWithStock, profitWithoutStock-prices[i])
profitWithoutStock = max(profitWithoutStock, profitWithStock+prices[i])
Copy link

Choose a reason for hiding this comment

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

これでも結果的には問題がないですが、本当は、この2変数は同時に更新したほうが意味としては自然に思います。

profitWithStock (株を持った状態で夜を越すときの持ち金の最大値。)
profitWithoutStock (株を持たない状態で夜を越すときの持ち金の最大値。)

なのだから、「前日、株を持った状態で夜を越すときの持ち金の最大値 + 株を売った利益」としたほうが自然に思います。結果的にはこれでもいいのですが。(それとも自然な解釈がありますでしょうか。)

Copy link
Owner Author

Choose a reason for hiding this comment

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

profitWithStock, profitWithoutStock = max(profitWithStock, profitWithoutStock-prices[i]), max(profitWithoutStock, profitWithStock+prices[i])

あるいは

lastProfitWithStock := profitWithStock
profitWithStock = max(profitWithStock, profitWithoutStock-prices[i])
profitWithoutStock = max(profitWithoutStock, lastProfitWithStock+prices[i])

ということですね。たしかに前日の値を元に当日の値を更新した方が自然ですね


```Go
func maxProfit(prices []int) int {
res := 0

Choose a reason for hiding this comment

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

個人的には変に省略語を使わずmaxProfitか、resultぐらいがいいと思います
最後まで読まないとresが何かわからないので

(Goの慣習だったらすいません)

Copy link
Owner Author

Choose a reason for hiding this comment

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

resやresult以外で名前をつけるとしたらmaxProfitかなと思いましたが、関数名と被るので避けました
Goの慣習については、スタイルガイドに

Function and method names should not use a Get or get prefix, unless the underlying concept uses the word “get” (e.g. an HTTP GET). Prefer starting the name with the noun directly, for example use Counts over GetCounts.

とあるので関数名をgetMaxProfit、変数名をmaxProfitにするのも微妙かなと思いました。
関数名をcomputeMaxProfit、変数名をmaxProfitあるいはaccumulatedProfitにするのも一つの選択肢かなと思います


### CS
- Rust
- なんで人気なんだろうと気になった

Choose a reason for hiding this comment

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

仕事で使うんですが、コンパイラがかなり賢くて関数型言語っぽい型計算ができたりします

Copy link
Owner Author

Choose a reason for hiding this comment

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

C/C++と比べて型推論に任せられる場面が多いということですか?(RustはC/C++と比較されることが多いと感じたので引き合いに出してみました)

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.

3 participants