-
Notifications
You must be signed in to change notification settings - Fork 0
53. Maximum Subarray #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ryosuketc
wants to merge
2
commits into
main
Choose a base branch
from
53_maximum_subarray
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # 53. Maximum Subarray | ||
|
|
||
| https://leetcode.com/problems/maximum-subarray/ | ||
|
|
||
| ## Comments | ||
|
|
||
| ### step1 | ||
|
|
||
| * 解いたことあるような気はしつつ、解法暗記しているわけではないので exmple 見ながら考えてみた。 | ||
| * 愚直に実装するなら O(n^2) (区間を決めて、その区間ごとに sum を再計算すると O(n^3) だが、running sum を持っておけば O(n^2) になる、あるいは累積和の配列を作ってもよい) | ||
| * とはいえ subarray == 連続する要素の array で、sliding window の問題として解けそう | ||
| * 例を使っていくつかシミュレーションしてみる。要は window をいつリセットするかで、それまでの累積和が、その単一の要素より小さければよいだろうというあたりをつけた | ||
| * 脳内デバッガをちゃんと回すべきだったが、`step1.SolutionWA` のように書いて WA | ||
| * `max_subarray_sum = 0` ではなく `nums[i]`。`[-1]` みたいな配列で 0 を返してしまう | ||
| * `if subarray_sum < 0` の条件が必要。`[1, 2]` で `2` (3 ではなく) になってしまう | ||
| * `SolutionAC` で 9:00 くらいで AC | ||
|
|
||
| ### step2 | ||
|
|
||
| * https://github.com/ryosuketc/leetcode_arai60/pull/45/files | ||
| * ああ、DP の問題でもあったのか。Kadane's Algorithm とか。今回はちょっと深煎りしないけど。 | ||
| * `Solution1`: max 使えばもっとシンプルに書ける (if 分岐を消せる) | ||
| * https://github.com/konnysh/arai60/pull/4#discussion_r1835271603 | ||
| * `INT_MIN` とかそういえばあったが、`std::numeric_limits<int>::min()` がモダン。最近使ってないので、C++ 番 `math.inf` どう書くのか忘れていた | ||
| * https://cpprefjp.github.io/reference/climits/int_min.html | ||
| * https://cpprefjp.github.io/reference/limits/numeric_limits.html | ||
| * `Solution2`: 一応これが一番シンプルかな | ||
|
|
||
| ### step3 | ||
|
|
||
| * skip |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| class Solution { | ||
| public: | ||
| int maxSubArray(vector<int>& nums) { | ||
| if (nums.empty()) { | ||
| return 0; // error | ||
| } | ||
| int max_subarray_sum = 0; | ||
| int subarray_sum = nums[0]; | ||
| for (int i = 1; i < nums.size(); ++i) { | ||
| int num = nums[i]; | ||
| if (subarray_sum < num) { | ||
| subarray_sum = num; // Reset the window | ||
| max_subarray_sum = std::max(max_subarray_sum, subarray_sum); | ||
| continue; | ||
| } | ||
| subarray_sum += num; | ||
| max_subarray_sum = std::max(max_subarray_sum, subarray_sum); | ||
| } | ||
| return max_subarray_sum; | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| class SolutionAC { | ||
| public: | ||
| int maxSubArray(vector<int>& nums) { | ||
| if (nums.empty()) { | ||
| return 0; // error | ||
| } | ||
| int max_subarray_sum = nums[0]; | ||
| int subarray_sum = nums[0]; | ||
| for (int i = 1; i < nums.size(); ++i) { | ||
| int num = nums[i]; | ||
| if (subarray_sum < 0 && subarray_sum < num) { | ||
| subarray_sum = num; // Reset the window | ||
| max_subarray_sum = std::max(max_subarray_sum, subarray_sum); | ||
| continue; | ||
| } | ||
| subarray_sum += num; | ||
| max_subarray_sum = std::max(max_subarray_sum, subarray_sum); | ||
| } | ||
| return max_subarray_sum; | ||
| } | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| class Solution1 { | ||
| public: | ||
| int maxSubArray(vector<int>& nums) { | ||
| if (nums.empty()) { | ||
| return 0; // error | ||
| } | ||
| int max_subarray_sum = nums[0]; | ||
| int subarray_sum = nums[0]; | ||
| for (int i = 1; i < nums.size(); ++i) { | ||
| int num = nums[i]; | ||
| subarray_sum = std::max(num, subarray_sum + num); | ||
| max_subarray_sum = std::max(max_subarray_sum, subarray_sum); | ||
| } | ||
| return max_subarray_sum; | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| class Solution { | ||
| public: | ||
| int maxSubArray(vector<int>& nums) { | ||
| if (nums.empty()) { | ||
| return 0; // error | ||
| } | ||
| int max_subarray_sum = std::numeric_limits<int>::min(); | ||
| int subarray_sum = 0; | ||
| for (auto num : nums) { | ||
| subarray_sum = std::max(num, subarray_sum + num); | ||
| max_subarray_sum = std::max(max_subarray_sum, subarray_sum); | ||
| } | ||
| return max_subarray_sum; | ||
| } | ||
| }; | ||
Empty file.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自分なら
や
subarray_sum = std::max(0, subarray_sum); subarray_sum += num;と書くのですが、趣味の範囲だと思います。