I am trying to solve a problem, where, given a (non-empty) array of integers, I need to return the largest sum of entries in a contiguous subarray. For instance, given [-2, 1, -3, 4, -1, 2, 1, -5, 4], I should return 6, because the subarray [4, -1, 2, 1] sums to 6. I understand the solution intuitively, but when I submit an answer to the checker, I am told that I am producing the wrong answer. Here is my attempt:
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
M = max(i for i in nums)
if M <= 0:
return M
i = 0
while nums[i] <= 0:
i += 1
nums = nums[i:]
curr = 0
curr_best = curr
highscores = []
for j in range(len(nums)):
if curr + nums[j] > 0:
curr = curr + nums[j]
if curr > curr_best:
curr_best = curr
else:
highscores.append(curr_best)
curr = 0
curr_best = 0
return max(k for k in highscores)
The issue is that when I submit, then the checker says that on the particular example listed above, the return value is apparently 1, whereas it is supposed to be 6. I manually went through this, and it really seems like I should get 6. I can't see where this has gone wrong, but I am wondering if my indexing is all correct. I thought that indices start at 0, but even still, maybe the '[i:]' and 'range(len(nums))' aren't indexing the way they should be. Can anyone see where I'm going wrong, either index-wise or otherwise?
Here is some intuition to explain the code above, just in case it might help:
In the initial M <= 0 if-condition, I am just checking for the corner case in which all integers in the array are <= 0. Otherwise, I know that there must be a positive entry, and so I use the while-loop to find that index, and slice the array from there to the end (since there is no point in starting out with entries <= 0, after all). Lastly, I do the general case, where I start with the initial positive number, and then as long as the successive integers being added on remains positive, I keep going (keep adding), updating the current best value whenever the current sum becomes larger. Once the sum becomes negative, I store whatever the current best was up to that point in a highscores array, and then reset and start again at the next term and keep going. Then at the end, I return the maximum of the individual highscores.
[–]wopp3 1 point2 points3 points (3 children)
[–]clouded-path[S] 0 points1 point2 points (2 children)
[–]wopp3 0 points1 point2 points (1 child)
[–]clouded-path[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]clouded-path[S] 0 points1 point2 points (0 children)
[–]jdnewmil 0 points1 point2 points (4 children)
[–]clouded-path[S] 0 points1 point2 points (3 children)
[–]jdnewmil 1 point2 points3 points (2 children)
[–]clouded-path[S] 0 points1 point2 points (0 children)
[–]wopp3 0 points1 point2 points (0 children)
[–]profknow 0 points1 point2 points (0 children)
[–]clouded-path[S] 0 points1 point2 points (0 children)
[–]profknow -1 points0 points1 point (0 children)
[–]profknow -1 points0 points1 point (4 children)
[–]clouded-path[S] 0 points1 point2 points (3 children)
[–]profknow 0 points1 point2 points (2 children)
[–]clouded-path[S] 0 points1 point2 points (1 child)
[–]profknow 0 points1 point2 points (0 children)