Open
Conversation
potrue
reviewed
Feb 21, 2026
| for (int i = 2; i < nums.size(); ++i) { | ||
| max_returns[i] = std::max(max_returns[i - 2] + nums[i], max_returns[i - 1]); | ||
| } | ||
|
|
potrue
reviewed
Feb 21, 2026
| int current_max_return = std::max(nums[0], nums[1]); | ||
|
|
||
| int second_previous_max_return = nums[0]; | ||
| int previous_max_return = current_max_return; |
There was a problem hiding this comment.
時系列的に、second_previousから書き始めた方が読みやすいかもしれないと思いました。
あと、second_previous, previousは0で初期化してしまってfor文をi=0からはじめても動く気がします。そうすると最初のearly returnも消せますかね。
Owner
Author
There was a problem hiding this comment.
ありがとうございます!おっしゃる通りで、シンプルになりました。
#include <vector>
class Solution {
public:
int rob(std::vector<int>& nums) {
int current_max_return = 0;
int previous_max_return = 0;
int second_previous_max_return = 0;
for (int i = 0; i < nums.size(); ++i) {
current_max_return = std::max(previous_max_return, second_previous_max_return + nums[i]);
second_previous_max_return = previous_max_return;
previous_max_return = current_max_return;
}
return current_max_return;
}
};
potrue
reviewed
Feb 21, 2026
| previous_max_return = current_max_return; | ||
| } | ||
|
|
||
| return current_max_return; |
There was a problem hiding this comment.
この変数(current_max_return)は保存しておく必要はない気がします(for文の中身を除いて常にprevious_max_returnと同一になるので。)
好みかもしれませんが、for文の中でのみ一時的な変数を定義して、最後のreturnはprevious_max_returnを返してしまっていいと思います。
Owner
Author
There was a problem hiding this comment.
ありがとうございます。確かにそうなのですが、temp があるよりも意味のある名前がついた current_max_return がある方がわかりやすいかなと思いました。
mamo3gr
reviewed
Feb 23, 2026
| return nums[0]; | ||
| } | ||
|
|
||
| // Maximum amount of money that can be robbed if you rob each house |
There was a problem hiding this comment.
if you rob each house
ここの意味が取れませんでした。文字通り読むと(隣り合わせの制約を無視して)すべての家から盗む、ということかなと思ったのですが、それだと用途と異なるようにも思えて…。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題
https://leetcode.com/problems/house-robber/
次の問題
213. House Robber 2