-
Notifications
You must be signed in to change notification settings - Fork 0
53. Maximum Subarray #34
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
skypenguins
wants to merge
1
commit into
main
Choose a base branch
from
leetcode/arai60/problem-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,102 @@ | ||
| # 53. Maximum Subarray | ||
| - 問題: https://leetcode.com/problems/maximum-subarray/ | ||
| - 言語: Python | ||
|
|
||
| - おおよそ15分以内に解答する | ||
|
|
||
| ## Step1 | ||
| - subarrayなので要素は連続している | ||
| - 最初、順不同(組合せ)と勘違いしていた | ||
| - 候補となるsubarrayを取得するwindowを考え、その大きさを元の配列の大きさまでインクリメントしていき、window内の要素の合計を候補の最大値とする | ||
| - 同じ部分和の計算を何度も実行しているので、これを解消してTLEを回避したいが15分経っていたので正答を見る | ||
|
|
||
| ### TLEとなるコード | ||
| ```py | ||
| class Solution: | ||
| def maxSubArray(self, nums: List[int]) -> int: | ||
| if len(nums) == 1: | ||
| return nums[0] | ||
|
|
||
| window_size = 1 | ||
| start = 0 | ||
| end = 0 | ||
| result = min(nums) | ||
| while window_size <= len(nums): | ||
| result = max(result, sum(nums[start:end+1])) | ||
| if end == len(nums) - 1: | ||
| start = 0 | ||
| end = window_size | ||
| window_size += 1 | ||
| else: | ||
| start += 1 | ||
| end += 1 | ||
|
|
||
| return result | ||
| ``` | ||
| - 時間計算量: $O(n^{3})$ っぽいので、 $10^{4}$ あたりになるとTLE | ||
|
|
||
| ### 正答 | ||
| - ある時点までで最大の連続和を更新していく | ||
| - Kadane法というらしい | ||
| - SWEの常識に入る? | ||
| - > 名前がついている上に常識に入っていない程度のもの | ||
| - cf. https://discord.com/channels/1084280443945353267/1206101582861697046/1207405733667410051 | ||
| - 時間計算量: $O(n)$ | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def maxSubArray(self, nums: List[int]) -> int: | ||
| current_sum = nums[0] | ||
| max_sum = nums[0] | ||
|
|
||
| for num in nums[1:]: | ||
| current_sum = max(num, current_sum + num) | ||
| max_sum = max(max_sum, current_sum) | ||
|
|
||
| return max_sum | ||
| ``` | ||
|
|
||
| ## Step2 | ||
| - 典型コメント集: https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.qgjy53psjkn2 | ||
|
|
||
| - https://discord.com/channels/1084280443945353267/1206101582861697046/1208414507735453747 | ||
| - > ちなみに、なんでこの問題が難しいかというと、何かをさせながら、何かをさせながら、何かをさせるからです。 | ||
| - https://discord.com/channels/1084280443945353267/1206101582861697046/1208414507735453747 | ||
| - Kadane法に至るまでの思考経路 | ||
| - https://discord.com/channels/1084280443945353267/1206101582861697046/1209351966329667585 | ||
| - 思考実験として、シフト勤務で引き継ぎ資料を残すとしたら何を残すか? | ||
|
|
||
| - https://github.com/sakupan102/arai60-practice/pull/33 | ||
| - Python | ||
| - $O(n)$ で解く方法 | ||
| - 累積和の配列を事前に計算、`nums[j]` ~ `nums[i-1]` の区間の和は `prefix_sums[i] - prefix_sums[j]` で求める | ||
| - 最小累積和の配列を事前に計算。「ここまでで最も小さかった累積和」を記録し続ける | ||
| - 最大部分配列を探す。区間の和 = `prefix_sums[i] - min_prefix_sums[i-1]` $(j < i)$ | ||
| - 2次元空間への拡張といった発展問題に強いと思った | ||
| - 変数名から累積和であることが読み取りづらかった | ||
|
|
||
| - https://github.com/olsen-blue/Arai60/pull/32 | ||
| - Python | ||
| - > 後ろを振り返った時に、最も標高の低い地点との差を求め、その差でmax()の最大値更新を行い続ければ良い。 | ||
| - この考え方はしっくりきた | ||
| - DPを意識した書き方を見て、Kadane法はDPであることに気づいた | ||
|
|
||
| ## Step3 | ||
| - Kadene法で解答 | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def maxSubArray(self, nums: List[int]) -> int: | ||
| current_sum = nums[0] | ||
| max_sum = nums[0] | ||
|
|
||
| for num in nums[1:]: | ||
| current_sum = max(num, current_sum + num) | ||
| max_sum = max(max_sum, current_sum) | ||
|
|
||
| return max_sum | ||
| ``` | ||
| - 所要時間 | ||
| - 1回目: 1:51 | ||
| - 2回目: 1:05 | ||
| - 3回目: 1:10 | ||
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.
leetcode上で時間切れになることより、実行時間がどれくらいかが重要だと思います。
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.
LC上での制限に意識が取られすぎていたかもしれません。ご指摘ありがとうございます。