Maximum Subarray Leetcode Solution Python

Given an integer array nums, find the subarray with the largest sum, and return its sum.

Leetcode Solution Python (Kadane’s Algorithm)

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        # Kadane's Algorithm
        max_sum = float('-inf')
        curr_sum = 0
        for i in range(0,len(nums)):
            curr_sum = max(nums[i], curr_sum+nums[i])
            max_sum = max(curr_sum, max_sum)
        return max_sum

Cracking the Coding Interview: 189 Programming Questions and Solutions

Author: mathtuition88

Math and Education Blog

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.