Skip to content
LC-2789 Medium LeetCode

2789. Largest Element in an Array after Merge Operations

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

# greedy
class Solution(object):
    def maxArrayValue(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        result = curr = 0
        for i in reversed(xrange(len(nums))):
            if nums[i] > curr:
                curr = 0
            curr += nums[i]
            result = max(result, curr)
        return result

Solution from kamyu104/LeetCode-Solutions · MIT