-
Notifications
You must be signed in to change notification settings - Fork 0
213. House Robber II #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| Solve Time: 18:45 | ||
|
|
||
| Time Complexity: O(n) | ||
| Space Complexity: O(n) | ||
|
|
||
| ある程度の方針は最初に建てられたが、正確なコードに落とし込む過程でミスを連発して時間がかかってしまった。 | ||
| 最後の家を盗むかどうかの判定に、最初の家を盗んだかどうかの記録が必要になるので、2通りの配列を用意してそこで管理する。 | ||
|
|
||
| */ | ||
| class Solution { | ||
| public: | ||
| int rob(vector<int>& nums) { | ||
| if (nums.size() == 1){ | ||
| return nums.front(); | ||
| } | ||
|
|
||
| if (nums.size() == 2){ | ||
| return max(nums[0], nums[1]); | ||
| } | ||
|
|
||
| vector<int>maxRobbedMoneyWithFirstRobbed(nums.size(), 0); | ||
| maxRobbedMoneyWithFirstRobbed[0] = nums[0]; | ||
| maxRobbedMoneyWithFirstRobbed[1] = nums[0]; | ||
| vector<int>maxRobbedMoneyWithoutFirstRobbed(nums.size(), 0); | ||
| maxRobbedMoneyWithoutFirstRobbed[1] = nums[1]; | ||
|
|
||
| for (int i = 2; i < nums.size(); ++i) { | ||
| if (i < nums.size() - 1) { | ||
| maxRobbedMoneyWithFirstRobbed[i] = max(maxRobbedMoneyWithFirstRobbed[i - 1], maxRobbedMoneyWithFirstRobbed[i - 2] + nums[i]); | ||
| } else { | ||
| maxRobbedMoneyWithFirstRobbed[i] = maxRobbedMoneyWithFirstRobbed[i - 1]; | ||
| } | ||
|
|
||
| maxRobbedMoneyWithoutFirstRobbed[i] = max(maxRobbedMoneyWithoutFirstRobbed[i - 1], maxRobbedMoneyWithoutFirstRobbed[i - 2] + nums[i]); | ||
| } | ||
|
|
||
| return max(maxRobbedMoneyWithoutFirstRobbed.back(), maxRobbedMoneyWithFirstRobbed.back()); | ||
|
|
||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
|
|
||
| 2変数 * 2で行うパターン | ||
| 必要な情報を詰め込んで変数名が長くなってしまっているのが難点 | ||
| */ | ||
|
|
||
| class Solution { | ||
| public: | ||
| int rob(vector<int>& nums) { | ||
| if(nums.size() == 1) { | ||
| return nums[0]; | ||
| } | ||
| if(nums.size() == 2) { | ||
| return max(nums[0], nums[1]); | ||
| } | ||
|
|
||
| int maxMoneyTwoPreRobWithFirstRob = nums[0]; | ||
| int maxMoneyOnePreRobWithFirstRob = nums[0]; | ||
|
|
||
| int maxMoneyTwoPreRobWithoutFirstRob = 0; | ||
| int maxMoneyOnePreRobWithoutFirstRob = nums[1]; | ||
|
|
||
| for (int i = 2; i < nums.size(); ++i) { | ||
| if (i < nums.size() - 1){ | ||
| int maxBenefitWithFirstRob = max(maxMoneyOnePreRobWithFirstRob, maxMoneyTwoPreRobWithFirstRob + nums[i]); | ||
| maxMoneyTwoPreRobWithFirstRob = maxMoneyOnePreRobWithFirstRob; | ||
| maxMoneyOnePreRobWithFirstRob = maxBenefitWithFirstRob; | ||
| } | ||
|
|
||
| int maxBenefitWithoutFirstRob = max(maxMoneyOnePreRobWithoutFirstRob, maxMoneyTwoPreRobWithoutFirstRob + nums[i]); | ||
| maxMoneyTwoPreRobWithoutFirstRob = maxMoneyOnePreRobWithoutFirstRob; | ||
| maxMoneyOnePreRobWithoutFirstRob = maxBenefitWithoutFirstRob; | ||
| } | ||
| return max(maxMoneyOnePreRobWithFirstRob, maxMoneyOnePreRobWithoutFirstRob); | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| https://github.com/5ky7/arai60/pull/37 | ||
| を参考にstd::spanを使用。 | ||
| vector<int>をコピーせずにいい感じに渡す方法を探したらこのPRに辿り着いた。 | ||
| */ | ||
| class Solution { | ||
| public: | ||
| int rob(vector<int>& nums) { | ||
| if (nums.size() == 1) { | ||
| return nums.front(); | ||
| } | ||
|
|
||
| std::span<int> with_first_house(nums.begin(), nums.end() - 1); | ||
| std::span<int> without_first_house(nums.begin() + 1, nums.end()); | ||
| return max(rob_linear(with_first_house), rob_linear(without_first_house)); | ||
| } | ||
|
|
||
| private: | ||
| int rob_linear(std::span<int>& nums) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. spanは元々lightweight viewなので&は必要ないかと思います |
||
| if (nums.size() == 1) { | ||
| return nums.front(); | ||
| } | ||
| int robbed_2_previous_max_money = nums[0]; | ||
| int robbed_previous_max_money = nums[1]; | ||
| for(int i = 2; i < nums.size(); ++i) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if for のあとにスペースが空いているものと空いていないものとが混ざっているのが気になりました。空けるほうに統一するとよいと思います。 参考までにスタイルガイドへのリンクを共有いたします。 https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace
なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。 |
||
| int max_money = max(robbed_2_previous_max_money + nums[i], robbed_previous_max_money); | ||
| robbed_2_previous_max_money = max(robbed_2_previous_max_money, robbed_previous_max_money); | ||
| robbed_previous_max_money = max_money; | ||
| } | ||
| return max(robbed_previous_max_money, robbed_2_previous_max_money); | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
>のあとにスペースを空けるほうが多いと思います。