Conversation
|
|
||
| - ドキュメント系 | ||
| - pythonの lru_cache https://github.com/python/cpython/blob/3.13/Lib/functools.py#L577 | ||
| - ドキュメントってどうやって読んでいけば良いのだろう(読めるようになる進歩の過程があまりイメージできない)、とりあえず"Doubly Linked List"とコメントがあって、実際に双方向連結リストを使っているのか、と確認した |
There was a problem hiding this comment.
たぶん、読み方は色々あるんですが、一つは機能からだいたいの構造を想像するというのも一つかもしれません。基本的には部分まで絞られていたら頭から読んでいます。
There was a problem hiding this comment.
ありがとうございます。
ある程度機能のアタリがついていて、それに合致していそうなところを見つけたら、その部分を頭からしっかり読んでいくという感じでしょうか。読むイメージがついて参考になります
| 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); | ||
| } |
There was a problem hiding this comment.
好みの範囲だと思いますが、以下のようにして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_stealingThere was a problem hiding this comment.
ありがとうございます。
確かに、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]); |
There was a problem hiding this comment.
DP配列であるhouse_to_gain[i]は、i番目までの家を盗んだ時の利得の最大値を表しているので、この比較は不要で、house_to_gain.back()で足りることに気づきました
There was a problem hiding this comment.
たしかにそうですね。
あと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) { |
There was a problem hiding this comment.
おっしゃるとおりです、失念していました
| return 0; | ||
| } | ||
|
|
||
| int gain_by_stealing= nums[0]; |
There was a problem hiding this comment.
= の両側にスペースを空けることをお勧めします。
参考までに 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) { |
There was a problem hiding this comment.
, の前にスペースを空けず、 , のあとにスペースを空けることをお勧めします。
| } | ||
| int RobHelper(int house, const std::vector<int>& nums, std::map<int ,int>& house_to_gain) { | ||
| if (house == 0) { | ||
| return nums[0]; |
This Problem
House Robber
Next Problem
House Robber II