Skip to content

198. House Robber#33

Open
hemispherium wants to merge 1 commit into
mainfrom
0198-house-robber
Open

198. House Robber#33
hemispherium wants to merge 1 commit into
mainfrom
0198-house-robber

Conversation

@hemispherium
Copy link
Copy Markdown
Owner

@hemispherium hemispherium self-assigned this Apr 23, 2026
class Solution {
public:
int rob(vector<int>& nums) {
vector<int> sum(nums.size());
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

2 つ前の値までしか参照していないため、変数を 2 つ使ってループのイテレーション間で値を伝搬することで、追加の空研計算量を O(1) とすることがあります。もし余裕があれば試してみるとよいと思います。

int rob(vector<int>& nums) {
vector<int> sum(nums.size());
for (int i = 0; i < nums.size(); i++) {
if (i == 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.

i == 0 のケースと i == 1 のケースをループの外で処理することで、ループの内容をシンプルにすることができると思います。

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.

そうですね、i==0とi==1のケースはfor文の外に出した方がよさそうです。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

もしくは nums のコピーを代入することでも達成できます

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.

3 participants