mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-02 21:22:43 +00:00
docs(docs): add O(n) Kadane implementation for Maximum Subarray solution
This commit is contained in:
1 parent
e63d517c9a
commit
667bc06965
1 file changed
+10
@@ -40,4 +40,14 @@ If you have figured out the O(n) solution, try coding another solution using the
|
||||
```py
|
||||
class Solution:
|
||||
def maxSubArray(self, nums: list[int]) -> int:
|
||||
best, total = nums[0], nums[0]
|
||||
|
||||
for i in range(1, len(nums)):
|
||||
if total < 0:
|
||||
total = nums[i]
|
||||
else:
|
||||
total = total + nums[i]
|
||||
best = max(best, total)
|
||||
|
||||
return best
|
||||
```
|
||||
Reference in new issue
Block a user