Maximum Subarray - Leetcode 53 Python - Kadane's Algorithm

preview_player
Показать описание
🔴 Question Link -

✅Connect with me

🔴 Question with Example

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Рекомендации по теме
Комментарии
Автор

Updated Soution:

class Solution:
def maxSubArray(self, nums: List[int]) -> int:
running_sum = float('-inf')
max_sum = nums[0]

for n in nums:
running_sum = max(running_sum+n, n)
max_sum = max(running_sum, max_sum)
return max_sum

PersistentProgrammer
Автор

nice explanation ; especially that two bucket! Thanks ! This Chanel deserves much more follower !!

timlee
Автор

The explanation was very helpful! Thank you

SW-nxjz
Автор

Please correct at 8:26. How could -2+1 = 1 ?

gattuchandrashekar