LeetCode 1788. Maximize the Beauty of the Garden
Prefix Sum
There is a garden of n
flowers, and each flower has an integer beauty value. The flowers are arranged in a line. You are given an integer array flowers
of size n
and each flowers[i]
represents the beauty of the ith
flower.
A garden is valid if it meets these conditions:
The garden has at least two flowers.
The first and the last flower of the garden have the same beauty value.
As the appointed gardener, you have the ability to remove any (possibly none) flowers from the garden. You want to remove flowers in a way that makes the remaining garden valid. The beauty of the garden is the sum of the beauty of all the remaining flowers.
Return the maximum possible beauty of some valid garden after you have removed any (possibly none) flowers.
Example 1:
Example 2:
Example 3:
Constraints:
2 <= flowers.length <= 10^5
-10^4 <= flowers[i] <= 10^4
It is possible to create a valid garden by removing some (possibly none) flowers.
Solution:
Last updated