-
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?
Conversation
profitWithStock = max(profitWithStock, profitWithoutStock-prices[i]) | ||
profitWithoutStock = max(profitWithoutStock, profitWithStock+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.
これでも結果的には問題がないですが、本当は、この2変数は同時に更新したほうが意味としては自然に思います。
profitWithStock (株を持った状態で夜を越すときの持ち金の最大値。)
profitWithoutStock (株を持たない状態で夜を越すときの持ち金の最大値。)
なのだから、「前日、株を持った状態で夜を越すときの持ち金の最大値 + 株を売った利益」としたほうが自然に思います。結果的にはこれでもいいのですが。(それとも自然な解釈がありますでしょうか。)
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.
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 |
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
か、result
ぐらいがいいと思います
最後まで読まないとres
が何かわからないので
(Goの慣習だったらすいません)
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.
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 | ||
- なんで人気なんだろうと気になった |
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.
仕事で使うんですが、コンパイラがかなり賢くて関数型言語っぽい型計算ができたりします
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.
C/C++と比べて型推論に任せられる場面が多いということですか?(RustはC/C++と比較されることが多いと感じたので引き合いに出してみました)
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/