Skip to content

53. maximum subarray#33

Open
5ky7 wants to merge 5 commits intomainfrom
53.-Maximum-Subarray
Open

53. maximum subarray#33
5ky7 wants to merge 5 commits intomainfrom
53.-Maximum-Subarray

Conversation

@5ky7
Copy link
Copy Markdown
Owner

@5ky7 5ky7 commented Feb 24, 2026

5ky7 added 5 commits February 24, 2026 16:14
Clarified conditions for including nums[i] in the subarray.
Added explanation on optimizing space complexity for maximum subarray problem.
Added insights on Kadane's algorithm and alternative approaches to subarray sum calculations.
- 時間計算量O(n^2), 空間計算量O(1).
- `nums`に負の数が含まれていることを忘れて`max_sum_subarray`の初期値を0にしてしまった.`numeric_limits<int>::min()`に修正するも,(当然)TLE.
- 工夫を考える.
- 一つ小さいサイズの問題の解を参照するのは不可能ぽい.`[2, -1, 3]`に対して`[2,1]`の解は`[2]`だから,ここから`[2,-1,3]`を構築するのは無理そう.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
- 一つ小さいサイズの問題の解を参照するのは不可能ぽい.`[2, -1, 3]`に対して`[2,1]`の解は`[2]`だから,ここから`[2,-1,3]`を構築するのは無理そう.
- 一つ小さいサイズの問題の解を参照するのは不可能ぽい.`[2, -1, 3]`に対して`[2,-1]`の解は`[2]`だから,ここから`[2,-1,3]`を構築するのは無理そう.

if (nums.empty()) {
return 0;
}
std::vector<int> max_subarray_with_itself(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.

with_itself の意味が掴みにくかったです( it が指すものが不明)。
おそらく nums[i] を末尾とする、が意図だと思うので、ending_with などどうでしょうか。

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.

ありがとうございます.確かにここは変数名の名付けに迷ったところでしたが,ending_withなどはわかりやすいですね.

Comment on lines +70 to +73
max_subarray_with_itself[i] = nums[i];
if (max_subarray_with_itself[i - 1] > 0) {
max_subarray_with_itself[i] += max_subarray_with_itself[i - 1];
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
max_subarray_with_itself[i] = nums[i];
if (max_subarray_with_itself[i - 1] > 0) {
max_subarray_with_itself[i] += max_subarray_with_itself[i - 1];
}
max_subarray_with_itself[i] = std::max(
nums[i],
nums[i] + max_subarray_with_itself[i - 1],
)

も自然言語での説明に近くて良さそうに感じました。

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.

2 participants