array and hashmap December 17, 2022

Maximum sum score of array

Time O(n) Space O(1) Open original problem

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)