Open
Conversation
Clarified conditions for including nums[i] in the subarray.
Added insights on Kadane's algorithm and alternative approaches to subarray sum calculations.
mamo3gr
reviewed
Feb 27, 2026
| - 時間計算量O(n^2), 空間計算量O(1). | ||
| - `nums`に負の数が含まれていることを忘れて`max_sum_subarray`の初期値を0にしてしまった.`numeric_limits<int>::min()`に修正するも,(当然)TLE. | ||
| - 工夫を考える. | ||
| - 一つ小さいサイズの問題の解を参照するのは不可能ぽい.`[2, -1, 3]`に対して`[2,1]`の解は`[2]`だから,ここから`[2,-1,3]`を構築するのは無理そう. |
There was a problem hiding this comment.
Suggested change
| - 一つ小さいサイズの問題の解を参照するのは不可能ぽい.`[2, -1, 3]`に対して`[2,1]`の解は`[2]`だから,ここから`[2,-1,3]`を構築するのは無理そう. | |
| - 一つ小さいサイズの問題の解を参照するのは不可能ぽい.`[2, -1, 3]`に対して`[2,-1]`の解は`[2]`だから,ここから`[2,-1,3]`を構築するのは無理そう. |
mamo3gr
reviewed
Feb 27, 2026
| if (nums.empty()) { | ||
| return 0; | ||
| } | ||
| std::vector<int> max_subarray_with_itself(nums.size()); |
There was a problem hiding this comment.
with_itself の意味が掴みにくかったです( it が指すものが不明)。
おそらく nums[i] を末尾とする、が意図だと思うので、ending_with などどうでしょうか。
Owner
Author
There was a problem hiding this comment.
ありがとうございます.確かにここは変数名の名付けに迷ったところでしたが,ending_withなどはわかりやすいですね.
mamo3gr
reviewed
Feb 27, 2026
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]; | ||
| } |
There was a problem hiding this comment.
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], | |
| ) |
も自然言語での説明に近くて良さそうに感じました。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題: 53. Maximum Subarray53. Maximum Subarray