Open
Conversation
naoto-iwase
reviewed
Nov 22, 2025
| } | ||
|
|
||
| private: | ||
| int CaluculateMaxMoney(const vector<int>& houses, int left, int right) { |
There was a problem hiding this comment.
このメソッド名ですが、robメソッドも最大金額を計算する点では共通していてrobとの違いが分かりにくいと感じるので、自分ならrob_sectionやrob_rangeとかにすると思います。
Comment on lines
+86
to
+90
| for (int i = left; i < right; ++i) { | ||
| int total_money = houses[i] + max_money_without_last; | ||
| max_money_without_last = max(max_money_without_last, max_money_with_last); | ||
| max_money_with_last = total_money; | ||
| } |
There was a problem hiding this comment.
ここは一時変数を置く方法のほか、同時更新する方法もありますね。
private:
int CaluculateMaxMoney(const vector<int>& houses, int start, int end) {
int max_money_with_last = 0;
int max_money_without_last = 0;
for (int i = start; i < end; ++i) {
UpdateMoney(max_money_without_last, max_money_with_last, houses[i]);
}
return max(max_money_with_last, max_money_without_last);
}
void UpdateMoney(int& max_money_without_last, int& max_money_with_last, int val) {
tie(max_money_without_last, max_money_with_last) = make_pair(
max(max_money_without_last, max_money_with_last),
max_money_without_last + val
);
}
};更新ロジックを試しに切り出してみたんですが、あんまり良くないかもしれません...
naoto-iwase
reviewed
Nov 22, 2025
Comment on lines
+127
to
+134
| int max_money_with_last = 0; | ||
| int max_money_without_last = 0; | ||
| for (int i = start; i < end; ++i) { | ||
| int current_total = houses[i] + max_money_without_last; | ||
| max_money_without_last = max(max_money_without_last, max_money_with_last); | ||
| max_money_with_last = current_total; | ||
| } | ||
| return max(max_money_with_last, max_money_without_last); |
There was a problem hiding this comment.
細かいですが、max_money_without_last, max_money_with_lastの初期化とreturnでmax()の引数に渡す順番は、更新順序と合わせた方が自然な気がします。
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/
次の問題
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/