Maximize the Beauty of the Garden - Problem

Imagine you're a master gardener tasked with creating the most beautiful garden possible! ๐ŸŒธ

You have a garden of n flowers arranged in a line, where each flower has an integer beauty value. Your goal is to create a valid garden by potentially removing some flowers, then calculate the maximum possible total beauty.

What makes a garden valid?

  • It must have at least 2 flowers
  • The first and last flowers must have the same beauty value

You're given an array flowers where flowers[i] represents the beauty of the i-th flower. You can remove any flowers you want (or none at all) to maximize the beauty sum of the remaining valid garden.

Example: If you have flowers [1, 2, 3, 1, 5], you could keep the subsequence [1, 2, 3, 1] (beauty sum = 7) or [1, 5, 1] by rearranging... wait, no rearranging allowed! You must maintain the original order.

Input & Output

example_1.py โ€” Basic Valid Garden
$ Input: [1, 2, 3, 1, 5]
โ€บ Output: 7
๐Ÿ’ก Note: We can create a valid garden with flowers [1, 2, 3, 1] by keeping indices 0-3. The first and last flowers both have value 1, and the total beauty is 1 + 2 + 3 + 1 = 7. We could also choose just [1, 1] for beauty 2, but 7 is optimal.
example_2.py โ€” Multiple Valid Gardens
$ Input: [2, -3, 4, -1, 2, 6]
โ€บ Output: 4
๐Ÿ’ก Note: The optimal valid garden is [2, -3, 4, -1, 2] using indices 0-4. First and last flowers both have value 2, and total beauty is 2 + (-3) + 4 + (-1) + 2 = 4. Other valid options like just [2, 2] would give beauty = 4, but removing the middle elements doesn't help here.
example_3.py โ€” No Valid Garden
$ Input: [1, 2, 3, 4, 5]
โ€บ Output: 0
๐Ÿ’ก Note: No two flowers have the same beauty value, so we cannot create a valid garden where the first and last flowers match. Return 0.

Constraints

  • 2 โ‰ค flowers.length โ‰ค 105
  • -104 โ‰ค flowers[i] โ‰ค 104
  • The garden must have at least 2 flowers
  • First and last flowers must have the same beauty value

Visualization

Tap to expand
๐ŸŒธ Garden Beauty Maximization ๐ŸŒธOriginal Garden12315Step 1: Identify Matching Flowers12315Matching pair!Step 2: Calculate Garden Beauty1231Beauty = 1 + 2 + 3 + 1 = 7๐ŸŽฏ One-Pass Hash Solution: O(n) time, O(n) spaceTrack first occurrence + running sum = Instant garden beauty calculation!
Understanding the Visualization
1
Identify Matching Pairs
Find flowers with the same beauty value that could serve as garden boundaries
2
Calculate Garden Beauty
For each valid pair, sum all flowers between them (inclusive)
3
Optimize with Hash Table
Use hash map to remember first occurrence and avoid recalculation
4
One-Pass Solution
Combine hash lookup with running sum for maximum efficiency
Key Takeaway
๐ŸŽฏ Key Insight: By combining hash table lookup with running prefix sums in a single pass, we can instantly calculate the beauty of any valid garden formed by matching flower pairs, achieving optimal O(n) time complexity.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
58.0K Views
High Frequency
~18 min Avg. Time
1.5K 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