1788. Maximize the Beauty of the Garden

Обновлено: 2024-03-12
1 мин
[]

Содержание

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