Maximum Total Beauty of the Gardens Problem
Description
LeetCode Problem 2234.
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 i^th 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 be0.
Return the maximum total beauty that Alice can obtain after planting at most newFlowers flowers.
Example 1:
Input: flowers = [1,3,1,1], newFlowers = 7, target = 6, full = 12, partial = 1
Output: 14
Explanation: Alice can plant
- 2 flowers in the 0^th garden
- 3 flowers in the 1^st garden
- 1 flower in the 2^nd garden
- 1 flower in the 3^rd garden
The gardens will then be [3,6,2,2]. She planted a total of 2 + 3 + 1 + 1 = 7 flowers.
There is 1 garden that is complete.
The minimum number of flowers in the incomplete gardens is 2.
Thus, the total beauty is 1 * 12 + 2 * 1 = 12 + 2 = 14.
No other way of planting flowers can obtain a total beauty higher than 14.
Example 2:
Input: flowers = [2,4,5,3], newFlowers = 10, target = 5, full = 2, partial = 6
Output: 30
Explanation: Alice can plant
- 3 flowers in the 0^th garden
- 0 flowers in the 1^st garden
- 0 flowers in the 2^nd garden
- 2 flowers in the 3^rd garden
The gardens will then be [5,4,5,5]. She planted a total of 3 + 0 + 0 + 2 = 5 flowers.
There are 3 gardens that are complete.
The minimum number of flowers in the incomplete gardens is 4.
Thus, the total beauty is 3 * 2 + 4 * 6 = 6 + 24 = 30.
No other way of planting flowers can obtain a total beauty higher than 30.
Note that Alice could make all the gardens complete but in this case, she would obtain a lower total beauty.
Constraints:
1 <= flowers.length <= 10^51 <= flowers[i], target <= 10^51 <= newFlowers <= 10^101 <= full, partial <= 10^5
Solution
Approach: Enumeration + Binary Search
A garden that already has at least target flowers is complete and never changes. Among the others, it is best to complete the ones with the most flowers, since they need the fewest new flowers, and to spend what is left on raising the minimum of the incomplete ones.
Sort flowers and build prefix sums. Enumerate x, the number of complete gardens, starting from the number that are already complete. Making the largest x gardens complete costs max(0, target - flowers[n - x]) for each new one; subtract it from newFlowers and stop when it goes negative.
The remaining flowers go to the other n - x gardens. Binary search for the largest index l such that raising the first l + 1 gardens to flowers[l] costs at most what is left: cost = flowers[l] * (l + 1) - s[l + 1]. The leftover is shared equally among those gardens, so the minimum becomes min(flowers[l] + (newFlowers - cost) / (l + 1), target - 1), rounded down. The total beauty for this x is x * full + minimum * partial. Return the best over all x.
Complexity: O(n log n) time and O(n) space.
from bisect import bisect_left
from itertools import accumulate
class Solution:
def maximumBeauty(
self, flowers: List[int], newFlowers: int, target: int, full: int, partial: int
) -> int:
flowers.sort()
n = len(flowers)
s = list(accumulate(flowers, initial=0))
ans, i = 0, n - bisect_left(flowers, target)
for x in range(i, n + 1):
newFlowers -= 0 if x == 0 else max(target - flowers[n - x], 0)
if newFlowers < 0:
break
l, r = 0, n - x - 1
while l < r:
mid = (l + r + 1) >> 1
if flowers[mid] * (mid + 1) - s[mid + 1] <= newFlowers:
l = mid
else:
r = mid - 1
y = 0
if r != -1:
cost = flowers[l] * (l + 1) - s[l + 1]
y = min(flowers[l] + (newFlowers - cost) // (l + 1), target - 1)
ans = max(ans, x * full + y * partial)
return ans