[Python] Leetcode Test Case: Maximum Subarray

Discussão em 'Python' iniciado por Stack, Setembro 4, 2025.

  1. Stack

    Stack Membro Participativo

    I was working on Leetcode 53: Maximum Subarray. I wrote a brute force solution, but Leetcode keeps saying me result is 0 for test cases like nums = [-1] or nuts = [-1, -2]. Below is my code:

    def maxSubArray(nums):
    maxSum = - (10**5)
    for i in range(1,len(nums) + 1):
    left = 0
    right = left + i
    while right <= (len(nums) + 1):
    s = sum(nums[left:right])
    maxSum = max(s, maxSum)
    left +=1
    right += 1
    return maxSum


    I have also attached a screenshot of the error see here. Where is 0 coming from? Please let me know, thank you!

    I know Kadane's algorithm, but I just want to understand why this gives me 0. When I ran the code and the test case on my local machine and Python visualizer, it gives me the right result, but somehow Leetcode just outputs 0.

    Continue reading...

Compartilhe esta Página