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 leasttarget 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 _themaximum total beauty that Alice can obtain after planting at most _newFlowersflowers.
Input: flowers =[1,3,1,1], newFlowers =7, target =6, full =12, partial =1Output: 14Explanation: Alice can plant
-2 flowers in the 0th garden
-3 flowers in the 1st garden
-1 flower in the 2nd garden
-1 flower in the 3rd garden
The gardens will then be [3,6,2,2]. She planted a total of 2+3+1+1=7 flowers.There is1 garden that is complete.The minimum number of flowers in the incomplete gardens is2.Thus, the total beauty is1*12+2*1=12+2=14.No other way of planting flowers can obtain a total beauty higher than 14.
Input: flowers =[2,4,5,3], newFlowers =10, target =5, full =2, partial =6Output: 30Explanation: Alice can plant
-3 flowers in the 0th garden
-0 flowers in the 1st garden
-0 flowers in the 2nd garden
-2 flowers in the 3rd 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 is4.Thus, the total beauty is3*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 inthiscase, she would obtain a lower total beauty.
To maximize total beauty, we want to maximize the number of complete gardens (with at least target flowers) and, for incomplete gardens, maximize the minimum number of flowers among them. By sorting the gardens and using binary search, we can efficiently distribute new flowers to achieve the best result.
classSolution {
public:longlong maximumBeauty(vector<int>& flowers, longlong newFlowers, int target, int full, int partial) {
int n = flowers.size();
sort(flowers.begin(), flowers.end());
for (int& x : flowers) x = min(x, target);
vector<longlong> pre(n+1);
for (int i =0; i < n; ++i) pre[i+1] = pre[i] + flowers[i];
longlong ans =0;
for (int c =0; c <= n; ++c) {
longlong rem = newFlowers;
if (c) rem -= target * c - (pre[n] - pre[n-c]);
if (rem <0) continue;
int l =0, r = target-1, minInc =0;
while (l <= r) {
int m = (l + r) /2;
int idx = upper_bound(flowers.begin(), flowers.end()-c, m) - flowers.begin();
longlong need =1LL* m * idx - pre[idx];
if (need <= rem) {
minInc = m;
l = m +1;
} else {
r = m -1;
}
}
longlong cur =1LL* full * c + (c < n ?1LL* partial * minInc : 0);
ans = max(ans, cur);
}
return ans;
}
};
classSolution {
publiclongmaximumBeauty(int[] flowers, long newFlowers, int target, int full, int partial) {
int n = flowers.length;
Arrays.sort(flowers);
for (int i = 0; i < n; ++i) flowers[i]= Math.min(flowers[i], target);
long[] pre =newlong[n+1];
for (int i = 0; i < n; ++i) pre[i+1]= pre[i]+ flowers[i];
long ans = 0;
for (int c = 0; c <= n; ++c) {
long rem = newFlowers;
if (c > 0) rem -= target * c - (pre[n]- pre[n-c]);
if (rem < 0) continue;
int l = 0, r = target-1, minInc = 0;
while (l <= r) {
int m = (l + r) / 2;
int idx = upperBound(flowers, 0, n-c, m);
long need = 1L * m * idx - pre[idx];
if (need <= rem) {
minInc = m;
l = m + 1;
} else {
r = m - 1;
}
}
long cur = 1L * full * c + (c < n ? 1L * partial * minInc : 0);
ans = Math.max(ans, cur);
}
return ans;
}
privateintupperBound(int[] arr, int l, int r, int x) {
while (l < r) {
int m = (l + r) / 2;
if (arr[m]> x) r = m;
else l = m + 1;
}
return l;
}
}
classSolution {
funmaximumBeauty(flowers: IntArray, newFlowers: Long, target: Int, full: Int, partial: Int): Long {
val n = flowers.size
for (i in flowers.indices) flowers[i] = minOf(flowers[i], target)
flowers.sort()
val pre = LongArray(n+1)
for (i in0 until n) pre[i+1] = pre[i] + flowers[i]
var ans = 0Lfor (c in0..n) {
var rem = newFlowers
if (c > 0) rem -= target.toLong() * c - (pre[n] - pre[n-c])
if (rem < 0) continuevar l = 0; var r = target-1; var minInc = 0while (l <= r) {
val m = (l + r) / 2val idx = flowers.slice(0 until n-c).indexOfFirst { it > m }.let { if (it== -1) n-c elseit }
val need = m.toLong() * idx - pre[idx]
if (need <= rem) {
minInc = m
l = m + 1 } else {
r = m - 1 }
}
val cur = full.toLong() * c + if (c < n) partial.toLong() * minInc else0L ans = maxOf(ans, cur)
}
return ans
}
}
defmaximum_beauty(flowers: list[int], new_flowers: int, target: int, full: int, partial: int) -> int:
n = len(flowers)
flowers = [min(x, target) for x in flowers]
flowers.sort()
pre = [0] * (n+1)
for i in range(n):
pre[i+1] = pre[i] + flowers[i]
ans =0for c in range(n+1):
rem = new_flowers
if c:
rem -= target * c - (pre[n] - pre[n-c])
if rem <0:
continue l, r, min_inc =0, target-1, 0while l <= r:
m = (l + r) //2 idx = next((i for i, x in enumerate(flowers[:n-c]) if x > m), n-c)
need = m * idx - pre[idx]
if need <= rem:
min_inc = m
l = m +1else:
r = m -1 cur = full * c + (partial * min_inc if c < n else0)
ans = max(ans, cur)
return ans
impl Solution {
pubfnmaximum_beauty(flowers: Vec<i32>, new_flowers: i64, target: i32, full: i32, partial: i32) -> i64 {
let n = flowers.len();
letmut flowers: Vec<i32>= flowers.into_iter().map(|x| x.min(target)).collect();
flowers.sort();
letmut pre =vec![0i64; n+1];
for i in0..n { pre[i+1] = pre[i] + flowers[i] asi64; }
letmut ans =0i64;
for c in0..=n {
letmut rem = new_flowers;
if c >0 {
rem -= target asi64* c asi64- (pre[n] - pre[n-c]);
}
if rem <0 { continue; }
let (mut l, mut r, mut min_inc) = (0, target-1, 0);
while l <= r {
let m = (l + r) /2;
let idx = flowers[..n-c].iter().position(|&x| x > m).unwrap_or(n-c);
let need = m asi64* idx asi64- pre[idx];
if need <= rem {
min_inc = m;
l = m +1;
} else {
r = m -1;
}
}
let cur = full asi64* c asi64+if c < n { partial asi64* min_inc asi64 } else { 0 };
ans = ans.max(cur);
}
ans
}
}