Skip to content
LC-2640 Medium LeetCode

2640. Find the Score of All Prefixes of an Array

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 72% Topics: Array, Prefix Sum
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

# prefix sum
class Solution(object):
    def findPrefixScore(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        curr = 0
        for i in xrange(len(nums)):
            curr = max(curr, nums[i])
            nums[i] += (nums[i-1] if i-1 >= 0 else 0)+curr
        return nums

Solution from kamyu104/LeetCode-Solutions · MIT