Skip to content

198. House Robber#33

Open
seal-azarashi wants to merge 4 commits intomainfrom
house-robber
Open

198. House Robber#33
seal-azarashi wants to merge 4 commits intomainfrom
house-robber

Conversation

@seal-azarashi
Copy link
Owner

int maxAmount = 0;
for (int i = 0; i < amountCache.length; i++) {
if (i == 2) {
amountCache[i] += amountCache[i - 2];
Copy link

Choose a reason for hiding this comment

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

個人的には nums をコピーして amountCache にして、+= すると、amountCache の意味がループを回っている間に変わっていくので0で初期化して、構築していったほうが読みやすいと思います。

for (int i = 0; i < amountCache.length; i++) {
if (i == 2) {
amountCache[i] += amountCache[i - 2];
} else if (2 < i) {

Choose a reason for hiding this comment

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

iを先に持ってきて、他と書き方統一したいかなと思いました。

@Ryotaro25
Copy link

最適化されたボトムアップとトップダウンどちらも試されていて全体的にいいと思いました。

int[] amountCache = Arrays.copyOfRange(nums, 0, nums.length);
int maxAmount = 0;
for (int i = 0; i < amountCache.length; i++) {
if (i == 2) {
Copy link

@Yoshiki-Iwasa Yoshiki-Iwasa Oct 7, 2024

Choose a reason for hiding this comment

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

2がマジックナンバー化してるので、コメントがあると上から読んだとき理解しやすいと思います。

return 0;
}

int twoBackMaxCount = 0, oneBackMaxCount = 0;
Copy link

@goto-untrapped goto-untrapped Oct 12, 2024

Choose a reason for hiding this comment

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

twoBackMaxAmount でいいのかなと思ったのですが、twoBackMaxCount と使い分けている理由って何でしょうか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

@goto-untrapped
返信が大変遅くなりました。

特に意図があって使い分けてるわけではありませんでした。問題文にも "return the maximum amount of money ~" とあるので、amount の方がいいですね。修正して step 4 に記載しました。

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