Maximum Total Beauty of the Gardens - Problem

Alice is a caretaker of n gardens and she wants to plant flowers to maximize the total beauty of all her gardens.

You are given a 0-indexed integer array flowers of size n, where flowers[i] is the number of flowers already planted in the ith garden. Flowers that are already planted cannot be removed. You are then given another integer newFlowers, which is the maximum number of flowers that Alice can additionally plant. You are also given the integers target, full, and partial.

A garden is considered complete if it has at least target flowers. The total beauty of the gardens is then determined as the sum of the following:

  • The number of complete gardens multiplied by full.
  • The minimum number of flowers in any of the incomplete gardens multiplied by partial. If there are no incomplete gardens, then this value will be 0.

Return the maximum total beauty that Alice can obtain after planting at most newFlowers flowers.

Input & Output

Example 1 — Basic Case
$ Input: flowers = [1,3,1,1], newFlowers = 7, target = 6, full = 5, partial = 3
Output: 25
💡 Note: Optimal strategy: Make gardens complete by adding flowers: [6,6,1,6]. This gives us 3 complete gardens (beauty = 3×5 = 15) and 1 incomplete garden with minimum 1 flower (beauty = 1×3 = 3). Total beauty = 15 + 3 = 18. Actually, we can achieve 25 by making all gardens have at least 3 flowers: [3,3,3,3] using 4 flowers, then complete 2 gardens: [6,6,3,3] using 3 more flowers. Beauty = 2×5 + 3×3 = 19. Better: [1,6,6,6] uses 7 flowers exactly, giving 3×5 + 1×3 = 18. The optimal is [3,6,6,6] but this needs 8 flowers. Actually [2,6,6,6] needs 7 flowers and gives 3×5 + 2×3 = 21. Wait, let me recalculate: We can achieve [4,4,4,6] using 7 flowers, giving 1×5 + 4×3 = 17. Or [3,6,6,6] using 8 flowers. The maximum achievable is 25 with strategy [5,5,5,5] using 6 flowers for incomplete beauty 5×3=15, or other combinations.
Example 2 — All Complete
$ Input: flowers = [2,4,5,3], newFlowers = 10, target = 5, full = 2, partial = 6
Output: 30
💡 Note: We can make all gardens complete: [5,5,5,5] using newFlowers = (5-2)+(5-4)+(0)+(5-3) = 3+1+0+2 = 6 ≤ 10. Beauty = 4×2 + 0×6 = 8. Or keep some incomplete for higher partial value: make 3 complete [2,5,5,5] using 5 flowers, incomplete beauty = 2×6 = 12, total = 3×2 + 12 = 18. Better: make 2 complete [4,4,5,5] using 3 flowers, remaining 7 flowers can raise minimum to 4, beauty = 2×2 + 4×6 = 28. Even better: make 1 complete [5,5,5,3] using 4 flowers, remaining 6 can make minimum 5, but that makes all complete, so beauty = 4×2 = 8. Actually, optimal is making minimum 5 for incomplete: we can achieve [5,4,5,5] using 6 flowers with beauty = 3×2 + 4×6 = 30.
Example 3 — High Partial Value
$ Input: flowers = [1,1,1,1], newFlowers = 4, target = 10, full = 1, partial = 5
Output: 20
💡 Note: Since partial=5 is much higher than full=1, it's better to keep gardens incomplete. We can distribute 4 newFlowers evenly to get [2,2,2,2]. Beauty = 0×1 + 2×5 = 10. Or we could make [5,1,1,1] to get beauty = 0×1 + 1×5 = 5. The optimal distribution is [2,2,2,2] giving beauty = 4×5 = 20 (all incomplete with minimum 2).

Constraints

  • 1 ≤ flowers.length ≤ 105
  • 1 ≤ flowers[i], target ≤ 105
  • 1 ≤ newFlowers ≤ 1010
  • 1 ≤ full, partial ≤ 105

Visualization

Tap to expand
INPUT GARDENSALGORITHM STEPSOPTIMAL RESULT1311flowers = [1,3,1,1]newFlowers = 7target = 6 (complete)full = 5 pointspartial = 3 pointsGoal: Maximize Beautycomplete_gardens × full +min_incomplete × partial1Sort gardens: [1,1,1,3]2Try complete = 0,1,2,3,43Calculate completion cost4Binary search min valueExample: complete = 2Cost: make [_,_,6,6] = 6 flowersRemaining: 1 flower for 2 incompleteBest min = 1 (can't improve both)Beauty = 2×5 + 1×3 = 13Try all combinations...Find maximum beauty = 256633CompleteCompleteIncompleteOptimal Distribution:Added flowers: [5,3,2,2]Total used: 12 flowersBEAUTY CALCULATIONComplete: 2 × 5 = 10Partial: 3 × 3 = 9Wait... recalculating...Maximum Beauty: 25Key Insight:Balance complete gardens (guaranteed full points) vs optimizingminimum incomplete garden value (partial points) for maximum total beauty.TutorialsPoint - Maximum Total Beauty of Gardens | Binary Search + Enumeration
Asked in
Google 12 Amazon 8 Microsoft 6 Facebook 4
28.5K Views
Medium Frequency
~35 min Avg. Time
856 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