Maximum sum score of array
December 17, 2022
array-and-hashmapProblem URL: Maximum sum score of array
We will calculate the prefix sum of the array. Then we will iterate through the array, and for each element, we will find the maximum sum of the subarray that ends at the current element. Then we will update the maximum sum of the subarray that ends at the current element.
class Solution:
def maximumSumScore(self, nums: List[int]) -> int:
score = -math.inf
leftSum, rightSum = 0, sum(nums)
for num in nums:
leftSum += num
score = max(score, leftSum, rightSum)
rightSum -= num
return score
Time complexity: O(n)
Space complexity: O(1)