Open
Conversation
hroc135
reviewed
Feb 22, 2026
| int rob(std::vector<int>& nums) { | ||
| int num_house = nums.size(); | ||
|
|
||
| if (num_house == 1) { |
There was a problem hiding this comment.
num_house <= 3 の場合に max_element するのも一つの選択肢だと思います
potrue
reviewed
Feb 22, 2026
| class Solution { | ||
| public: | ||
| int rob(std::vector<int>& nums) { | ||
| int num_houses = nums.size(); |
There was a problem hiding this comment.
これぐらいだったら自分はnums.size()のまま使ってしまいそうです。
potrue
reviewed
Feb 22, 2026
|
|
||
| for (int i = start; i <= end; ++i) { | ||
| current_max_return = std::max(second_previous_max_return + nums[i], previous_max_return); | ||
|
|
There was a problem hiding this comment.
この改行は自分ならつけないですかね。L16-17にもついてないですし、重要なロジックの区切りがあるわけでもない気がします。
nodchip
reviewed
Feb 22, 2026
| std::vector<std::vector<int>> max_returns(nums.size(), std::vector<int>(2, 0)); | ||
|
|
||
| // Did not rob nums[0] | ||
| max_returns[0][0] = 0; |
There was a problem hiding this comment.
一次元目と二次元目の添え字が何を表しているのか分かりづらく感じました。二次元目の添え字を定数とするのが良いでしょうか。
mamo3gr
reviewed
Feb 27, 2026
| int second_previous_max_return = 0; | ||
|
|
||
| for (int i = start; i <= end; ++i) { | ||
| current_max_return = std::max(second_previous_max_return + nums[i], previous_max_return); |
There was a problem hiding this comment.
それぞれの変数の命名がややこなれていない印象を受けました。特に second_previous_ など。例えば、previous を言い換えて以下はどうでしょうか。
- max_return
- max_return_robbed_last
- max_return_skipped_last
mamo3gr
reviewed
Feb 27, 2026
| int previous_max_return = 0; | ||
| int second_previous_max_return = 0; | ||
|
|
||
| for (int i = start; i <= end; ++i) { |
There was a problem hiding this comment.
個人的には、end はexclusiveであるのが自然に思います。inclusiveなら first, last がしっくりきます。
Owner
Author
There was a problem hiding this comment.
ありがとうございます。
そういう使い分けの発想がなかったので参考になります。
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-ii/
次の問題
121. Best Time to Buy and Sell Stock