Skip to content

213. House Robber II#34

Open
hemispherium wants to merge 2 commits into
mainfrom
0213-house-robber-II
Open

213. House Robber II#34
hemispherium wants to merge 2 commits into
mainfrom
0213-house-robber-II

Conversation

@hemispherium
Copy link
Copy Markdown
Owner

@hemispherium hemispherium self-assigned this Apr 23, 2026

### step3

3回通すまで書き直し。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

他の人のコードも見た方がいいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

承知しました!

public:
int rob(vector<int>& nums) {
int size = nums.size();
vector<vector<int>> sum(size, vector<int>(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.

first~last までの範囲で盗めるお金を最大化する関数を実装し、 0~nums.size()-2 と 1~nums.size()-1 の範囲で、それぞれ盗めるお金の最大値を算出し、 max() を取る実装のほうがシンプルになると思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。alternative-solution.cppで実装して追加してみました。

Comment on lines +7 to +9
if (i == 0) {
sum[i][0] = 0;
sum[i][1] = nums[i];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

forループの外に出して,forをi = 1から始める方が見やすいと感じました.

sum[0][0] = 0;
sum[0][0] = nums[0];
for (int i = 1; i < size; ++i) {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

たしかにこれはfor文の外に出してしまったほうがよさそうですね。ありがとうございます。

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.

4 participants