558. Maximize the Beauty of the Garden
Given an integer array flowers of size n, where flowers[i] denotes the beauty of the i-th flower. The flowers are positioned in a straight line
A valid garden must satisfy the following conditions:
- It contains at least two flowers.
- The first and last flower of the selected garden must have the same beauty value.
The task is to remove any number of flowers (including none) to create a valid garden. The beauty of a valid garden is the sum of the beauty values of all flowers in it. Return the maximum possible beauty of any valid garden that can be formed after removing flowers.
Example 1:
Input: flowers = [2, 4, 6, 2, 5]
Output: 14
Explanation:
You can select the valid garden [2, 4, 6, 2], where the first and last flowers have the same beauty value (2).
Total beauty = 2 + 4 + 6 + 2 = 14.
Example 2:
Input: flowers = [50, 5, 5, -10, 5]
Output: 15
Explanation:
The valid garden [5, 5, 5] gives a total beauty of 5 + 5 + 5 = 15.
Now Your Turn!
Pick the correct output for the given inputInput: flowers = [-2, -4, 0, -2]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- n == flowers.length
- 2 ≤ n ≤ 105
- -105 ≤ flowers[i] ≤ 105