Skip to content

Conversation

@jiku0730
Copy link
Owner

@jiku0730 jiku0730 commented Jun 5, 2025

int rob(int *nums, int numsSize)
{
int current_sum[2];
int take_house;
Copy link

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言語しかやったことがなく、その考えには至らなかったです。
この場合、どのような変数名が適切だと思いますか?
お時間があるときにご回答いただけますと幸いです。

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などどうでしょうか

Copy link

Choose a reason for hiding this comment

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

Fuminitonさんと同じような変数名にすると思います。

やっぱ最後の比較は必要やわ。
うーん、やっぱ全体的に見づらいし、理解も深くなさそう。。。

*
Copy link

Choose a reason for hiding this comment

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

解く順番にこだわりがなければ、他の人と同じように以下のリンクの順番で解いていくほうが学習効率が高いかも?と思いました。
https://1kohei1.com/leetcode/

Copy link
Owner Author

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;

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;

Choose a reason for hiding this comment

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

三項演算子はあまり推奨されていない印象です。
複数行を1行にまとまっているより、複数行でも上から読んでいってスッと理解できることの方が重要だと自分は思います。

discordに色々な議論がありますので、検索してみるといいと思います。

Copy link

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 はちょっとやりにくいですね。

Copy link
Owner Author

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];
Copy link

@Fuminiton Fuminiton Jun 5, 2025

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.

たしかに、ご指摘ありがとうございます。

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