Skip to content

198. House Robber#38

Open
ryoooooory wants to merge 1 commit intomainfrom
task/198
Open

198. House Robber#38
ryoooooory wants to merge 1 commit intomainfrom
task/198

Conversation

@ryoooooory
Copy link
Copy Markdown
Owner

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

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
Copy Markdown

@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
Copy Markdown

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.

5 participants