Skip to content

198. House Robber #38

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

198. House Robber #38

wants to merge 1 commit into from

Conversation

ryoooooory
Copy link
Owner

if (nums.length == 0) {
return 0;
}
int[][] currentMaxSum = new int[nums.length][2];
Copy link

Choose a reason for hiding this comment

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

私は、これ2変数に分けます。[0], [1] の意味は読み手にとってパズルですよね。

int[] memo = new int[nums.length];
Arrays.fill(memo, -1);
memo[0] = nums[0];
robHelper(nums, memo, nums.length - 1);
Copy link

@fuga-98 fuga-98 Jun 9, 2025

Choose a reason for hiding this comment

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

ヘルパー関数には返り値があるのにそれを受け取っていない点がひっかかりました。
副作用を利用して動くのであれば、それが分かるような変数名やコメントを書いてほしいと感じました。

副作用に関して
olsen-blue/Arai60#29 (comment)

public int rob(int[] nums) {
if (nums.length == 0) {
return 0;
}
Copy link

Choose a reason for hiding this comment

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

単独の if 文と if-else if が混ざっているのがやや違和感がありました。いずれも比較される対象が nums.length のため、統一した書き方で書いたほうが、読んでいて混乱が少なくなると思いました。 else if で十分だと思います。

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.

4 participants