Maximize the Beauty of the Garden - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: flowers = [1,3,2,1,4]
Output: 8
💡 Note: We can create a valid garden using flowers at indices 0 and 3 (both have beauty 1). The garden includes flowers [1,3,2,1] with total beauty = 1+3+2+1 = 7. Actually, we want the maximum, so we check all possibilities and find that keeping [1,3,2,1] gives us 7.
Example 2 — Multiple Options
$ Input: flowers = [2,4,2,4]
Output: 12
💡 Note: We have two options: garden with first/last flower = 2 gives sum 2+4+2+4 = 12, or garden with flowers at indices 1,3 (value 4) gives sum 4+2+4 = 10. Maximum is 12.
Example 3 — No Valid Garden
$ Input: flowers = [1,2,3,4]
Output: 0
💡 Note: No two flowers have the same beauty value, so we cannot create a valid garden. Return 0.

Constraints

  • 2 ≤ flowers.length ≤ 104
  • -104 ≤ flowers[i] ≤ 104

Visualization

Tap to expand
Maximize the Beauty of the Garden INPUT flowers array (n=5) 1 i=0 3 i=1 2 i=2 1 i=3 4 i=4 Garden Visualization: 1 3 2 1 4 Same beauty value = 1 Valid Garden Conditions: 1. At least 2 flowers 2. First and last have same value ALGORITHM STEPS 1 Build Hash Map Store first occurrence index for each beauty value 2 Compute Prefix Sum Only count positive values for middle flowers 3 Find Matching Pairs For each value, find first and current occurrence 4 Calculate Maximum Sum = 2*value + prefix between positions Hash Map (first index): value: 1 --> index: 0 value: 3 --> index: 1 value: 2 --> index: 2 value: 4 --> index: 4 Match found: value=1 at i=0,3 FINAL RESULT Valid Garden Selected: 1 3 2 1 Flower at index 4 (value=4) removed from garden Beauty Calculation: First flower: 1 Middle sum: 3 + 2 = 5 Last flower: 1 Total: 1 + 5 + 1 = 7 Better: Include flower 4 1 + 3 + 2 + 1 = 7... but Output: 8 [1,3,2,1,4]--> keep 4 too! Key Insight: Use a hash map to track the first occurrence of each beauty value. For each flower with the same value seen before, calculate the sum of positive values between them. The answer is 2*value plus the maximum prefix sum of positive middle flowers. Negative values in middle can be skipped! TutorialsPoint - Maximize the Beauty of the Garden | Hash Approach
Asked in
Google 15 Amazon 12 Meta 8 Apple 6
23.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen