From 81e21606daa9a53d774027ca5a3f829b5a25111f Mon Sep 17 00:00:00 2001 From: tom4649 Date: Fri, 27 Mar 2026 18:28:10 +0900 Subject: [PATCH 1/3] 53. Maximum Subarray --- 53/memo.md | 20 ++++++++++++++++++++ 53/sol1.py | 11 +++++++++++ 53/sol2.py | 9 +++++++++ 3 files changed, 40 insertions(+) create mode 100644 53/memo.md create mode 100644 53/sol1.py create mode 100644 53/sol2.py diff --git a/53/memo.md b/53/memo.md new file mode 100644 index 0000000..8f98506 --- /dev/null +++ b/53/memo.md @@ -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って配列の最初からではなくてもそう呼ぶのかな +- 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) + + diff --git a/53/sol1.py b/53/sol1.py new file mode 100644 index 0000000..b4a046d --- /dev/null +++ b/53/sol1.py @@ -0,0 +1,11 @@ +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + max_sum = -float("inf") + cummulative_sum = 0 + min_cummulative_sum = 0 + for n in nums: + cummulative_sum += n + max_sum = max(max_sum, cummulative_sum - min_cummulative_sum) + min_cummulative_sum = min(min_cummulative_sum, cummulative_sum) + + return max_sum diff --git a/53/sol2.py b/53/sol2.py new file mode 100644 index 0000000..fc4c298 --- /dev/null +++ b/53/sol2.py @@ -0,0 +1,9 @@ +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + max_sum = -float("inf") + max_sum_end_with_current_n = -float("inf") + for n in nums: + max_sum_end_with_current_n = max(max_sum_end_with_current_n + n, n) + max_sum = max(max_sum, max_sum_end_with_current_n) + + return max_sum From 05b5f7736bdb0454f73f0c0ecdfc8cc3bcb994f4 Mon Sep 17 00:00:00 2001 From: tom4649 Date: Sun, 29 Mar 2026 05:11:16 +0900 Subject: [PATCH 2/3] Apply suggestion --- 53/sol1.py | 10 +++++----- 53/sol2.py | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/53/sol1.py b/53/sol1.py index b4a046d..8287230 100644 --- a/53/sol1.py +++ b/53/sol1.py @@ -1,11 +1,11 @@ class Solution: def maxSubArray(self, nums: List[int]) -> int: max_sum = -float("inf") - cummulative_sum = 0 - min_cummulative_sum = 0 + cumulative_sum = 0 + min_cumulative_sum = 0 for n in nums: - cummulative_sum += n - max_sum = max(max_sum, cummulative_sum - min_cummulative_sum) - min_cummulative_sum = min(min_cummulative_sum, cummulative_sum) + 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 diff --git a/53/sol2.py b/53/sol2.py index fc4c298..698033b 100644 --- a/53/sol2.py +++ b/53/sol2.py @@ -1,9 +1,9 @@ class Solution: def maxSubArray(self, nums: List[int]) -> int: max_sum = -float("inf") - max_sum_end_with_current_n = -float("inf") + max_ending_here = -float("inf") for n in nums: - max_sum_end_with_current_n = max(max_sum_end_with_current_n + n, n) - max_sum = max(max_sum, max_sum_end_with_current_n) + max_ending_here = max(n, max_ending_here + n) + max_so_far = max(max_so_far, max_ending_here) return max_sum From 0512416224ecd000e528cd126c6958ff4b5d3a75 Mon Sep 17 00:00:00 2001 From: tom4649 Date: Mon, 30 Mar 2026 03:19:41 +0900 Subject: [PATCH 3/3] Fix variable names --- 53/sol2.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/53/sol2.py b/53/sol2.py index 698033b..94a79ad 100644 --- a/53/sol2.py +++ b/53/sol2.py @@ -1,9 +1,9 @@ class Solution: def maxSubArray(self, nums: List[int]) -> int: - max_sum = -float("inf") - max_ending_here = -float("inf") + max_so_far = -float("inf") + max_ending_with_n = -float("inf") for n in nums: - max_ending_here = max(n, max_ending_here + n) - max_so_far = max(max_so_far, max_ending_here) + max_ending_with_n = max(n, max_ending_with_n + n) + max_so_far = max(max_so_far, max_ending_with_n) - return max_sum + return max_so_far