-
Notifications
You must be signed in to change notification settings - Fork 0
53. Maximum Subarray #30
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # 53. Maximum Subarray | ||
|
|
||
| - 愚直にsol1.pyを書いた | ||
| - 計算量 O(n) | ||
| - Kadaneのアルゴリズム | ||
| - https://en.wikipedia.org/wiki/Maximum_subarray_problem | ||
| - https://qiita.com/awesam/items/37a0ceb1468feef3a403 | ||
| - https://ark4rk.hatenablog.com/entry/2018/01/08/002508 | ||
| - 自然言語で説明すれば、「arrayを一巡し、各要素(a_iとする)で終わるものが最大値になりうるかを検証する。一つ前の要素で終わったsubarrayの最大値を記録しておけば、それにa_iを加えたものかa_iだけのsubarrayがa_iで終わったarrayの最大値である」 | ||
| - sol2.py | ||
| - https://discord.com/channels/1084280443945353267/1206101582861697046/1207518775851876362 | ||
| - sol1.pyと同じ考え | ||
| - https://github.com/mamo3gr/arai60/blob/53_maximum-subarray/53_maximum-subarray/step1.py | ||
| - cumulative sumって配列の最初からではなくてもそう呼ぶのかな | ||
|
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. 配列の最初から、とするのが一般的みたいですね。
Owner
Author
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とcumulative sumの区別があるのですね。 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. ご指摘の通り、リンク先のコードの
Owner
Author
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. 誤解を極力避けるという姿勢、勉強になります。 |
||
| - https://github.com/mamo3gr/arai60/blob/53_maximum-subarray/53_maximum-subarray/step3.py | ||
| - > マイナスなら相続をやめる | ||
| - https://github.com/mamo3gr/arai60/blob/53_maximum-subarray/53_maximum-subarray/step2.py | ||
| - 分割統治法, O(Nlog N) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class Solution: | ||
| def maxSubArray(self, nums: List[int]) -> int: | ||
| max_sum = -float("inf") | ||
| cumulative_sum = 0 | ||
| min_cumulative_sum = 0 | ||
| for n in nums: | ||
| cumulative_sum += n | ||
| max_sum = max(max_sum, cumulative_sum - min_cumulative_sum) | ||
| min_cumulative_sum = min(min_cumulative_sum, cumulative_sum) | ||
|
|
||
| return max_sum |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class Solution: | ||
| def maxSubArray(self, nums: List[int]) -> int: | ||
| max_so_far = -float("inf") | ||
| max_ending_with_n = -float("inf") | ||
| for n in nums: | ||
| max_ending_with_n = max(n, max_ending_with_n + n) | ||
| max_so_far = max(max_so_far, max_ending_with_n) | ||
|
|
||
| return max_so_far |
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.
nums[i]までの累積和をc[i]としたとき、i = 0 ... len(nums)-1を舐めながら最大となるc[i] - c[j] (j < i)を探す。つまりmin(c[j])を探しながら、maxをc[i] - c[j]で更新していく、というのが考え方だと思います。累積和の算出とこの更新が一度の走査でできる形で書けるのは「愚直」ではなく、それなりに工夫されていると感じました。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.
この意味はまあ正しくて、「見て思いついたまま」累積和の算出とこの更新を一度の走査でやるように書けた、というのがすごいなあと思った、という感想でした。言い方を改める必要は特に感じませんでした。