1788. Maximize the Beauty of the Garden

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1788

class Solution:
    def maximumBeauty(self, flowers: List[int]) -> int:
        s = [0] * (len(flowers) + 1)
        d = {}
        res = -inf
        for i, v in enumerate(flowers):
            if v in d:
                res = max(res, s[i] - s[d[v] + 1] + v * 2)
            else:
                d[v] = i
            s[i + 1] = s[i] + max(v, 0)
        return res