-
Notifications
You must be signed in to change notification settings - Fork 0
198. house robber #7
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
| int rob(int *nums, int numsSize) | ||
| { | ||
| int current_sum[2]; | ||
| int take_house; |
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言語しかやったことがなく、その考えには至らなかったです。
この場合、どのような変数名が適切だと思いますか?
お時間があるときにご回答いただけますと幸いです。
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.
入力に操作をする、あるいは操作をした結果を返すのがメソッドだと思うので、「操作の対象」は名詞になり、「操作をする」や「結果を返す」は動詞になるのが自然だと思います。
今回だと、1つ前までの最大利益と2つ前までの最大利益を使って、最大利益を求めるといったことがしたいわけなので、max_profit_1_ago, max_profit_2_agoなどどうでしょうか
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.
Fuminitonさんと同じような変数名にすると思います。
| やっぱ最後の比較は必要やわ。 | ||
| うーん、やっぱ全体的に見づらいし、理解も深くなさそう。。。 | ||
|
|
||
| * |
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.
解く順番にこだわりがなければ、他の人と同じように以下のリンクの順番で解いていくほうが学習効率が高いかも?と思いました。
https://1kohei1.com/leetcode/
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.
友人と共同でやっている関係でこのような順番になっています。
Arai60も参考にして問題を選んでみます。
ご提示いただきありがとうございます!
| int rob(int *nums, int numsSize) | ||
| { | ||
| int current_sum[2]; | ||
| int take_house; |
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.
入力に操作をする、あるいは操作をした結果を返すのがメソッドだと思うので、「操作の対象」は名詞になり、「操作をする」や「結果を返す」は動詞になるのが自然だと思います。
今回だと、1つ前までの最大利益と2つ前までの最大利益を使って、最大利益を求めるといったことがしたいわけなので、max_profit_1_ago, max_profit_2_agoなどどうでしょうか
| for (int i = 2; i < numsSize; i++) { | ||
| take_house = nums[i] + max_money_two_before; | ||
| not_take_house = nums[i - 1]; | ||
| max_money_two_before = max_money_one_before > max_money_two_before ? max_money_one_before : max_money_two_before; |
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.
三項演算子はあまり推奨されていない印象です。
複数行を1行にまとまっているより、複数行でも上から読んでいってスッと理解できることの方が重要だと自分は思います。
discordに色々な議論がありますので、検索してみるといいと思います。
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.
ここはちょっと整理して欲しいのですが、
自然言語で表現するときに、「A が B より大きいときは A を、そうでないときは B を A に代入する。」といいますかね。
これは if に直りますし、2つの引数の大きい方という関数を書いてもいいでしょう。
本当はリファレンスを読む癖をつけたいので、C はちょっとやりにくいですね。
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.
max関数を作成するなどしたほうが良かったですね。
3項演算子の使用は避けようと思います。
| int not_take_house; | ||
| for (int i = 2; i < numsSize; i++) { | ||
| take_house = nums[i] + current_sum[0]; | ||
| not_take_house = nums[i - 1]; |
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.
たしかに、ご指摘ありがとうございます。
This problem
https://github.com/Jikuhara/LeetCode/compare/198_house_robber?expand=1
Next problem
未定