-
Notifications
You must be signed in to change notification settings - Fork 0
198. House Robber #48
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,36 @@ | ||
| // Solve Time: 10:49 | ||
| // | ||
| // Space Complexity: O(n) | ||
| // Time Complexity: O(n) | ||
|
|
||
| /* | ||
|
|
||
| なんとなく方針をを決めてそのまま実装する | ||
| 配列を用意して、特定の位置における最大利益、とする。 | ||
| 同日に隣接する箇所は襲えないので、前日の値をそのまま持ってくるか、一昨日の利益に当日の利益を足すか、のどちらかを選ぶ。 | ||
|
|
||
| benefit += maximumBenefit[i - 2]; | ||
| とすべき箇所を | ||
| benefit += nums[i - 2]; | ||
| としてしまった | ||
|
|
||
| */ | ||
|
|
||
| class Solution { | ||
| public: | ||
| int rob(vector<int>& nums) { | ||
| vector<int> maximumBenefit(nums.size(), 0); | ||
| if (nums.size() == 1) { | ||
| return nums[0]; | ||
| } | ||
| maximumBenefit[0] = nums[0]; | ||
| for (int i = 1; i < nums.size(); ++i) { | ||
| int benefit = nums[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 (i >= 2) { | ||
| benefit += maximumBenefit[i - 2]; | ||
| } | ||
| maximumBenefit[i] = max(benefit, maximumBenefit[i - 1]); | ||
| } | ||
| return maximumBenefit.back(); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class Solution { | ||
| public: | ||
| int rob(vector<int>& nums) { | ||
| vector<int> maximumBenefit(nums.size(), 0); | ||
| maximumBenefit[0] = nums[0]; | ||
| for (int i = 1; i < maximumBenefit.size(); ++i) { | ||
| int benefit = nums[i]; | ||
| if (i >= 2) { | ||
| benefit += maximumBenefit[i - 2]; | ||
| } | ||
| maximumBenefit[i] = max(benefit, maximumBenefit[i - 1]); | ||
| } | ||
| return maximumBenefit.back(); | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // 2変数で行う手法 | ||
| class Solution { | ||
| public: | ||
| int rob(vector<int>& nums) { | ||
| if (nums.size() == 1) { | ||
| return nums.front(); | ||
| } | ||
| int twoPreviousMaximumRobbedMoney = nums[0]; | ||
|
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. Maximum という単語は、しばしば Max と略されるのを目にします。 Minimum も Min と略されるように思います。 |
||
| int previousMaximumRobbedMoney = max(nums[0], nums[1]); | ||
|
Comment on lines
+8
to
+9
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. こちらは2つの状態、A:「最後に盗んだのが2つ以上前の家であるとき
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. こちらなどにありますね. 一方で私は必ずしも排反にする必要性もないかな,と感じました.というのも今回は排反にすることで嬉しいことが起こるケースもありますが,今回は特に嬉しさは感じなかったためです. |
||
| for (int i = 2; i < nums.size(); ++i) { | ||
| int currentMaximumRobbedMoney = max(previousMaximumRobbedMoney, twoPreviousMaximumRobbedMoney + nums[i]); | ||
| twoPreviousMaximumRobbedMoney = previousMaximumRobbedMoney; | ||
| previousMaximumRobbedMoney = currentMaximumRobbedMoney; | ||
| } | ||
| return previousMaximumRobbedMoney; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class Solution { | ||
| public: | ||
| int rob(vector<int>& nums) { | ||
| if (nums.size() == 1) { | ||
| return nums[0]; | ||
| } | ||
| vector<int> maximumRobbedMoney(nums.size(), 0); | ||
| maximumRobbedMoney[0] = nums[0]; | ||
| maximumRobbedMoney[1] = max(nums[0], nums[1]); | ||
| for (int i = 2; i < nums.size(); ++i) { | ||
| maximumRobbedMoney[i] = max(maximumRobbedMoney[i - 1], maximumRobbedMoney[i - 2] + nums[i]); | ||
| } | ||
| return maximumRobbedMoney.back(); | ||
| } | ||
| }; |
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.
nums.size() == 1のみを例外処理している点に違和感を覚えました.というのも
nums.size() == 1でもこの行以降のコードは正しく動くからです.コーナーケースとして明示的に扱うなら,(問題の制約上あり得ないですが)nums.size() == 0(つまりnums.empty() == True)も考慮したコードの方が,実用的に感じました.