Skip to content

Create 0_198. House Robber.md#38

Open
irohafternoon wants to merge 1 commit intomainfrom
198.-House-Robber
Open

Create 0_198. House Robber.md#38
irohafternoon wants to merge 1 commit intomainfrom
198.-House-Robber

Conversation

@irohafternoon
Copy link
Copy Markdown
Owner

This Problem
House Robber
Next Problem
House Robber II


- ドキュメント系
- pythonの lru_cache https://github.com/python/cpython/blob/3.13/Lib/functools.py#L577
- ドキュメントってどうやって読んでいけば良いのだろう(読めるようになる進歩の過程があまりイメージできない)、とりあえず"Doubly Linked List"とコメントがあって、実際に双方向連結リストを使っているのか、と確認した
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.

ありがとうございます。
ある程度機能のアタリがついていて、それに合致していそうなところを見つけたら、その部分を頭からしっかり読んでいくという感じでしょうか。読むイメージがついて参考になります

int temp = gain_by_stealing;
gain_by_stealing = std::max(gain_by_stealing, gain_by_not_stealing + nums[i]);
gain_by_not_stealing = std::max(gain_by_not_stealing, temp);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

好みの範囲だと思いますが、以下のようにしてtempをなくすのもいいかなあと思いました。

int next_gain_by_stealing = std::max(gain_by_stealing, gain_by_not_stealing + nums[i]);
gain_by_not_stealing = std::max(gain_by_not_stealing, gain_by_stealing);
gain_by_stealing = next_gain_by_stealing

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.

ありがとうございます。
確かに、tempよりもこちらの方が今→次の遷移の意図がわかりやすくて良いですね、
もはや4つ変数を持ってswapする方法もあるのではと思ったのですが、ちょっと仰々しすぎるかもしれないと思っています

class Solution {
public:
    int rob(const std::vector<int>& nums) {
        if (nums.empty()) {
            return 0;
        }

        int gain_by_stealing= nums[0];
        int gain_by_not_stealing = 0;
        for (int i = 1; i < nums.size(); i++) {
            int next_gain_by_stealing = gain_by_not_stealing + nums[i];
            int next_gain_by_not_stealing = std::max(gain_by_stealing, gain_by_not_stealing);
            std::swap(gain_by_stealing, next_gain_by_stealing);
            std::swap(gain_by_not_stealing, next_gain_by_not_stealing);
        }
        return std::max(gain_by_stealing, gain_by_not_stealing);
    }
};

for (int i = 2; i < num_houses; i++) {
house_to_gain[i] = std::max(house_to_gain[i - 1], house_to_gain[i - 2] + nums[i]);
}
return std::max(house_to_gain[num_houses - 1], house_to_gain[num_houses - 2]);
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.

DP配列であるhouse_to_gain[i]は、i番目までの家を盗んだ時の利得の最大値を表しているので、この比較は不要で、house_to_gain.back()で足りることに気づきました

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

たしかにそうですね。
あとhousetogainの長さがnumhousesと一致していることを覚えていないといけないので自分ならhousetogain.size()-1のようにインデックスを指定すると思いました。(スマホにつきスネークケースで書くのをサボってすみません)

std::map<int, int> house_to_gain;
return RobHelper(nums.size() - 1, nums, house_to_gain);
}
int RobHelper(int house, const std::vector<int>& nums, std::map<int ,int>& house_to_gain) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

privateでいいのではないでしょうか?

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.

おっしゃるとおりです、失念していました

return 0;
}

int gain_by_stealing= nums[0];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

= の両側にスペースを空けることをお勧めします。

参考までに Google C++ Style Guide へのリンクを貼ります。
https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace

// Assignment operators always have spaces around them.
x = 0;

ただし、上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。

std::map<int, int> house_to_gain;
return RobHelper(nums.size() - 1, nums, house_to_gain);
}
int RobHelper(int house, const std::vector<int>& nums, std::map<int ,int>& house_to_gain) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

, の前にスペースを空けず、 , のあとにスペースを空けることをお勧めします。

}
int RobHelper(int house, const std::vector<int>& nums, std::map<int ,int>& house_to_gain) {
if (house == 0) {
return nums[0];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

インデントがずれているようです。

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