Skip to content

Create 53. Maximum Subarray.md#35

Open
irohafternoon wants to merge 1 commit intomainfrom
53.-Maximum-Subarray
Open

Create 53. Maximum Subarray.md#35
irohafternoon wants to merge 1 commit intomainfrom
53.-Maximum-Subarray

Conversation

@irohafternoon
Copy link
Copy Markdown
Owner

This Problem
Maximum Subarray
Next Problem
Unique Paths

};
```

番兵を使わず、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.

この変形をする場合、同値なコードにするには nums の長さが1以上かを確認する必要がありますね。
(必要かはともかく。)

for (int i = 0; i < nums.size(); i++) {
cumulative_sum += nums[i];
max_subarray_sum = std::max(max_subarray_sum, cumulative_sum - min_cumulative_sum_ever);
min_cumulative_sum_ever = std::min(min_cumulative_sum_ever, cumulative_sum);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

_ever がなくても伝わるかなと思いました。
他の解法も含めて全体的に読みやすかったです。

public:
int maxSubArray(std::vector<int>& nums) {
int diff_from_bottom = 0;
int max_subarray_sum = INT_MIN;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

INT_MIN は C 言語風に感じます。 C++ では std::numeric_limits::min() という書き方をよく見かけるように思います。チームの標準的な書き方に合わせることをお勧めいたします。

}
return *std::max_element(max_subarray_sum_at.begin(), max_subarray_sum_at.end());
}
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

いいと思います。
これまでの値という意味で、so_farも便利です。

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.

5 participants