Skip to content

Latest commit

 

History

History
69 lines (55 loc) · 1.62 KB

_53. Maximum Subarray.md

File metadata and controls

69 lines (55 loc) · 1.62 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : February 26, 2025

Last updated : February 26, 2025


Related Topics : Array, Divide and Conquer, Dynamic Programming

Acceptance Rate : 51.77 %


V1/V2

Standard appraoches

V3

Oneliner attempt to optimize since built-in functions are coded in C.


Solutions

Python

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        output = nums[0]
        max_sum = nums[0]
        for num in nums[1:] :
            max_sum = max(max_sum + num, num)
            output = max(max_sum, output)
        return output
class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        output = -inf
        max_sum = -inf
        for num in nums :
            max_sum = max(max_sum + num, num)
            output = max(max_sum, output)
        return output
class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        max_sum = -inf
        return max((max_sum := max(max_sum + num, num)) for num in nums)