Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions 213/step1.cpp
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);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

> のあとにスペースを空けるほうが多いと思います。

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());

}
};
36 changes: 36 additions & 0 deletions 213/step2.cpp
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);
}
};
32 changes: 32 additions & 0 deletions 213/step3.cpp
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) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if for のあとにスペースが空いているものと空いていないものとが混ざっているのが気になりました。空けるほうに統一するとよいと思います。

参考までにスタイルガイドへのリンクを共有いたします。

https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace

if (b) { // Space after the keyword in conditions and loops.

なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。

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);
}
};