class Solution:
def maximumBeauty(self, flowers: List[int], newFlowers: int, target: int, full: int, partial: int) -> int:
n = len(flowers)
flowers = [min(f, target) for f in flowers]
flowers.sort()
full_cnt = 0
while flowers and flowers[-1] == target:
flowers.pop()
full_cnt += 1
n = len(flowers)
pre_sum = [0]
for f in flowers:
pre_sum.append(pre_sum[-1] + f)
res = 0
for k in range(full_cnt, full_cnt + n + 1):
if k == full_cnt + n:
res = max(res, (k) * full)
break
need = (target - flowers[-1]) * (n - (k - full_cnt)) - (pre_sum[-1] - pre_sum[(k - full_cnt)])
if need > newFlowers:
continue
left = 0
right = target - 1
while left < right:
mid = (left + right + 1) // 2
idx = bisect.bisect_left(flowers, mid, 0, n - (k - full_cnt))
cost = mid * idx - pre_sum[idx]
if cost <= newFlowers - need:
left = mid
else:
right = mid - 1
res = max(res, k * full + left * partial)
return res
class Solution {
public:
long long maximumBeauty(vector<int>& flowers, long long newFlowers, int target, int full, int partial) {
int n = flowers.size();
for (int &f : flowers) f = min(f, target);
sort(flowers.begin(), flowers.end());
int full_cnt = 0;
while (!flowers.empty() && flowers.back() == target) {
flowers.pop_back();
full_cnt++;
}
n = flowers.size();
vector<long long> pre_sum(n + 1, 0);
for (int i = 0; i < n; ++i)
pre_sum[i + 1] = pre_sum[i] + flowers[i];
long long res = 0;
for (int k = full_cnt; k <= full_cnt + n; ++k) {
if (k == full_cnt + n) {
res = max(res, 1LL * k * full);
break;
}
long long need = 1LL * (target - flowers[n - (k - full_cnt) - 1]) * (n - (k - full_cnt)) - (pre_sum[n] - pre_sum[k - full_cnt]);
if (need > newFlowers) continue;
int left = 0, right = target - 1;
while (left < right) {
int mid = (left + right + 1) / 2;
int idx = lower_bound(flowers.begin(), flowers.begin() + n - (k - full_cnt), mid) - flowers.begin();
long long cost = 1LL * mid * idx - pre_sum[idx];
if (cost <= newFlowers - need) left = mid;
else right = mid - 1;
}
res = max(res, 1LL * k * full + 1LL * left * partial);
}
return res;
}
};
class Solution {
public long maximumBeauty(int[] flowers, long newFlowers, int target, int full, int partial) {
int n = flowers.length;
for (int i = 0; i < n; ++i)
flowers[i] = Math.min(flowers[i], target);
Arrays.sort(flowers);
int fullCnt = 0;
while (n > 0 && flowers[n - 1] == target) {
n--;
fullCnt++;
}
long[] preSum = new long[n + 1];
for (int i = 0; i < n; ++i)
preSum[i + 1] = preSum[i] + flowers[i];
long res = 0;
for (int k = fullCnt; k <= fullCnt + n; ++k) {
if (k == fullCnt + n) {
res = Math.max(res, 1L * k * full);
break;
}
long need = 1L * (target - flowers[n - (k - fullCnt) - 1]) * (n - (k - fullCnt)) - (preSum[n] - preSum[k - fullCnt]);
if (need > newFlowers) continue;
int left = 0, right = target - 1;
while (left < right) {
int mid = (left + right + 1) / 2;
int idx = Arrays.binarySearch(flowers, 0, n - (k - fullCnt), mid);
if (idx < 0) idx = -idx - 1;
long cost = 1L * mid * idx - preSum[idx];
if (cost <= newFlowers - need) left = mid;
else right = mid - 1;
}
res = Math.max(res, 1L * k * full + 1L * left * partial);
}
return res;
}
}
var maximumBeauty = function(flowers, newFlowers, target, full, partial) {
flowers = flowers.map(f => Math.min(f, target)).sort((a, b) => a - b);
let fullCnt = 0;
while (flowers.length > 0 && flowers[flowers.length - 1] === target) {
flowers.pop();
fullCnt++;
}
let n = flowers.length;
let preSum = [0];
for (let f of flowers) preSum.push(preSum[preSum.length - 1] + f);
let res = 0;
for (let k = fullCnt; k <= fullCnt + n; ++k) {
if (k === fullCnt + n) {
res = Math.max(res, k * full);
break;
}
let need = (target - flowers[flowers.length - (k - fullCnt) - 1]) * (n - (k - fullCnt)) -
(preSum[preSum.length - 1] - preSum[k - fullCnt]);
if (need > newFlowers) continue;
let left = 0, right = target - 1;
while (left < right) {
let mid = Math.floor((left + right + 1) / 2);
let idx = lowerBound(flowers, mid, 0, n - (k - fullCnt));
let cost = mid * idx - preSum[idx];
if (cost <= newFlowers - need) left = mid;
else right = mid - 1;
}
res = Math.max(res, k * full + left * partial);
}
return res;
function lowerBound(arr, target, start, end) {
let l = start, r = end;
while (l < r) {
let m = Math.floor((l + r) / 2);
if (arr[m] < target) l = m + 1;
else r = m;
}
return l;
}
};
flowers
where flowers[i]
represents the number of flowers in the i
-th garden. You also have newFlowers
new flowers you can plant across these gardens. Your goal is to maximize the overall "beauty" of the gardens.
The beauty is calculated as follows:
target
flowers, it is considered "full" and contributes full
points to the total beauty.partial
times the minimum number of flowers among these gardens.newFlowers
however you like, but cannot exceed target
flowers in any garden.target
flowers.newFlowers
among the gardens, which would be extremely inefficient. A brute-force solution would involve distributing flowers in all possible combinations, but this would not scale for large inputs.
Instead, we notice that:
target
flowers) is always beneficial because it gives a fixed, high reward (full
points).target
, since adding more does not increase beauty.
target
. These are already "full".
full
points for each full garden) + (partial
times the minimum in the rest, if any).flowers = [1, 3, 1]
newFlowers = 7
target = 6
full = 12
partial = 1
flowers = [1, 3, 1]
(all below target).[1, 1, 3]
.full_cnt = 0
.newFlowers
among gardens, which is exponential and infeasible for large inputs.
Optimized approach: