-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/53. Maximum Subarray #32
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
Satorien
wants to merge
1
commit into
main
Choose a base branch
from
solve/arai60_53
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
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,88 @@ | ||
| ## Step 1. Initial Solution | ||
|
|
||
| - パッと思いついたのは累積和を使う方法 | ||
| - prefix_sumを複数管理しておく必要はあまりなさそう | ||
| - 一つの変数で管理しておいて、マイナスになるなら捨ててしまう | ||
| - これで一旦実装してみたが、答えがマイナスになる場合を想定していなくてマイナスの場合にcontinueしてしまっていた | ||
| - continueせずにmaxの更新を行うように変更してギリギリ10分くらい | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def maxSubArray(self, nums: List[int]) -> int: | ||
| max_sum = nums[0] | ||
| prefix_sum = 0 | ||
| for num in nums: | ||
| prefix_sum += num | ||
| if prefix_sum < 0: | ||
| prefix_sum = 0 | ||
| continue | ||
| max_sum = max(max_sum, prefix_sum) | ||
| return max_sum | ||
| ``` | ||
|
|
||
| ### Complexity Analysis | ||
|
|
||
| - 時間計算量:O(n) | ||
| - 空間計算量:O(1) | ||
|
|
||
| ## Step 2. Alternatives | ||
|
|
||
| - LeetCodeのFollow upに書いてある分割統治法でやろうとしてみたがよく分からなかったので撤退 | ||
| - こういう書き方があるらしい | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def maxSubArray(self, nums: List[int]) -> int: | ||
| prefix_sums = [*nums] | ||
| suffix_sums = [*nums] | ||
| for i in range(1, len(nums)): | ||
| if prefix_sums[i-1] > 0: | ||
| prefix_sums[i] += prefix_sums[i-1] | ||
| for i in range(len(nums)-2, -1, -1): | ||
| if suffix_sums[i+1] > 0: | ||
| suffix_sums[i] += suffix_sums[i+1] | ||
| def maxSubArrayFromIndex(begin: int, end: int) -> int: | ||
| if begin == end: | ||
| return nums[begin] | ||
| middle = (begin + end) // 2 | ||
| return max( | ||
| maxSubArrayFromIndex(begin, middle), | ||
| maxSubArrayFromIndex(middle+1, end), | ||
| prefix_sums[middle] + suffix_sums[middle+1]) | ||
|
|
||
| return maxSubArrayFromIndex(0, len(nums)-1) | ||
| ``` | ||
|
|
||
| - prefix_sumsやsuffix_sumsを予め作っておくことで辛うじてO(n)になっているが、作らなければO(n log n)になるとのこと | ||
| - 分割統治するlog n回ごとにprefix/suffixの計算が必要 | ||
| - prefix_sumだけで書こうとすると難しそうだが一応できるらしい | ||
| - https://github.com/hayashi-ay/leetcode/pull/36/commits/5be8e6aaeb3c791a741578818022a836110239c5#diff-a6956948eb5bc3137495f6d70424f00900c8ba03395ebacccf937a76b9a4d988R53 | ||
| - 流れは追いやすかった | ||
| - 文字数が長かったり、インデックスを1から始めたりするのは読みづらい | ||
| - https://github.com/hayashi-ay/leetcode/pull/36/files#diff-a6956948eb5bc3137495f6d70424f00900c8ba03395ebacccf937a76b9a4d988R85 | ||
| - 考え方として標高という捉え方が出来るらしい | ||
| - https://github.com/olsen-blue/Arai60/pull/32/files#diff-a6956948eb5bc3137495f6d70424f00900c8ba03395ebacccf937a76b9a4d988R8 | ||
| - 感覚の問題だが、個人的には積み木を引き継ぐイメージ | ||
| - マイナスだったら当然引き継げない | ||
| - ただ、こういう理解だったからこそマイナスが解になる場合を逃してしまったのでイメージに頼りつつも実際のエッジケースは幅広く具体的に見たいところ | ||
| - prefix_sumについての意見があった | ||
| - https://github.com/fhiyo/leetcode/pull/33/files#r1667620608 | ||
| - 自分の認識では、prefix_sumsが配列でprefix_sumはその時点までの累積和という意味なので問題ないはず | ||
|
|
||
| ## Step 3. Final Solution | ||
|
|
||
| - max()を使わずにif文を使う方が関数呼び出しがない分速そう | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def maxSubArray(self, nums: List[int]) -> int: | ||
| max_sum = nums[0] | ||
| prefix_sum = 0 | ||
| for num in nums: | ||
| prefix_sum += num | ||
| if max_sum < prefix_sum: | ||
| max_sum = prefix_sum | ||
| if prefix_sum < 0: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 読みやすかったです。prefix_sum に関する処理をまとめてループ冒頭に書くのもありかなと思いました。 |
||
| prefix_sum = 0 | ||
| return max_sum | ||
| ``` | ||
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.
どのくらい早くなるか気になりました。
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.
katsukii/leetcode#10 (comment)
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.
確かに気になったので以下の方法で調べたところ、2倍以上違うようでした
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.
重要なのは比ではなくて時間です。
一回あたり、上(条件分岐 + 代入)が 26 ns、下(max + 代入)が 68 ns ですね。
概ね、Python の関数呼び出しは数十 ns くらいと思いますので妥当な結果でしょう。
こういう風に、秒で評価しましょう。