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