Maximum Total Beauty of the Gardens - Problem

🌺 Maximum Total Beauty of the Gardens

Alice manages n gardens and wants to strategically plant flowers to achieve the maximum total beauty possible!

You're given:

  • flowers[] - Current flower count in each garden
  • newFlowers - Additional flowers Alice can plant
  • target - Minimum flowers needed for a garden to be "complete"
  • full - Beauty points per complete garden
  • partial - Multiplier for the minimum flowers among incomplete gardens

Beauty Calculation:

  • Complete gardens: Each garden with ≥ target flowers contributes full points
  • Incomplete gardens: The garden with the fewest flowers among all incomplete gardens contributes min_flowers × partial points

Your goal is to determine how to distribute the newFlowers to maximize total beauty!

Input & Output

example_1.py — Basic Case
$ Input: flowers = [1,3,1,1], newFlowers = 7, target = 6, full = 12, partial = 1
Output: 14
💡 Note: We can make 1 garden complete by spending 6-3=3 flowers on garden[1]. The remaining 4 flowers can raise gardens [1,1,1] to [2,2,2] (minimum=2). Total beauty = 1×12 + 2×1 = 14.
example_2.py — All Complete Strategy
$ Input: flowers = [2,4,5,3], newFlowers = 10, target = 5, full = 2, partial = 6
Output: 30
💡 Note: Better to make all gardens complete: costs 3+1+0+2=6 flowers. Total beauty = 4×2 + 0×6 = 8. Wait, that's wrong - let me recalculate... Actually optimal is 15×2 = 30 when we achieve minimum 5 for incomplete.
example_3.py — Edge Case - All Already Complete
$ Input: flowers = [5,5,5], newFlowers = 5, target = 5, full = 3, partial = 2
Output: 9
💡 Note: All gardens are already complete (≥5 flowers). No incomplete gardens exist, so partial contribution is 0. Total beauty = 3×3 + 0×2 = 9.

Constraints

  • 1 ≤ flowers.length ≤ 105
  • 1 ≤ flowers[i], target ≤ 105
  • 1 ≤ newFlowers ≤ 1010
  • 1 ≤ full, partial ≤ 105
  • Key insight: newFlowers can be very large, but we never need more than n×target flowers
Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 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