Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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 least target flowers. The total beauty of the gardens is then determined as the sum of the following:
full.partial. If there are no incomplete gardens, then this value will be 0.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 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 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 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 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 <= 1051 <= flowers[i], target <= 1051 <= newFlowers <= 10101 <= full, partial <= 105Problem summary: 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 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 be 0. Return the maximum total beauty that Alice can obtain after planting
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Binary Search · Greedy
[1,3,1,1] 7 6 12 1
[2,4,5,3] 10 5 2 6
split-array-largest-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2234: Maximum Total Beauty of the Gardens
class Solution {
public long maximumBeauty(int[] flowers, long newFlowers, int target, int full, int partial) {
Arrays.sort(flowers);
int n = flowers.length;
long[] s = new long[n + 1];
for (int i = 0; i < n; ++i) {
s[i + 1] = s[i] + flowers[i];
}
long ans = 0;
int x = 0;
for (int v : flowers) {
if (v >= target) {
++x;
}
}
for (; x <= n; ++x) {
newFlowers -= (x == 0 ? 0 : Math.max(target - flowers[n - x], 0));
if (newFlowers < 0) {
break;
}
int l = 0, r = n - x - 1;
while (l < r) {
int mid = (l + r + 1) >> 1;
if ((long) flowers[mid] * (mid + 1) - s[mid + 1] <= newFlowers) {
l = mid;
} else {
r = mid - 1;
}
}
long y = 0;
if (r != -1) {
long cost = (long) flowers[l] * (l + 1) - s[l + 1];
y = Math.min(flowers[l] + (newFlowers - cost) / (l + 1), target - 1);
}
ans = Math.max(ans, (long) x * full + y * partial);
}
return ans;
}
}
// Accepted solution for LeetCode #2234: Maximum Total Beauty of the Gardens
func maximumBeauty(flowers []int, newFlowers int64, target int, full int, partial int) int64 {
sort.Ints(flowers)
n := len(flowers)
s := make([]int, n+1)
for i, x := range flowers {
s[i+1] = s[i] + x
}
ans := 0
i := n - sort.SearchInts(flowers, target)
for x := i; x <= n; x++ {
if x > 0 {
newFlowers -= int64(max(target-flowers[n-x], 0))
}
if newFlowers < 0 {
break
}
l, r := 0, n-x-1
for l < r {
mid := (l + r + 1) >> 1
if int64(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]+int((newFlowers-int64(cost))/int64(l+1)), target-1)
}
ans = max(ans, x*full+y*partial)
}
return int64(ans)
}
# Accepted solution for LeetCode #2234: Maximum Total Beauty of the Gardens
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
// Accepted solution for LeetCode #2234: Maximum Total Beauty of the Gardens
/**
* [2234] Maximum Total Beauty of the Gardens
*
* 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 be 0.
*
* 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^5
* 1 <= flowers[i], target <= 10^5
* 1 <= newFlowers <= 10^10
* 1 <= full, partial <= 10^5
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/maximum-total-beauty-of-the-gardens/
// discuss: https://leetcode.com/problems/maximum-total-beauty-of-the-gardens/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn maximum_beauty(
flowers: Vec<i32>,
new_flowers: i64,
target: i32,
full: i32,
partial: i32,
) -> i64 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2234_example_1() {
let flowers = vec![1, 3, 1, 1];
let new_flowers = 7;
let target = 6;
let full = 12;
let partial = 1;
let result = 14;
assert_eq!(
Solution::maximum_beauty(flowers, new_flowers, target, full, partial),
result
);
}
#[test]
#[ignore]
fn test_2234_example_2() {
let flowers = vec![2, 4, 5, 3];
let new_flowers = 10;
let target = 5;
let full = 2;
let partial = 6;
let result = 30;
assert_eq!(
Solution::maximum_beauty(flowers, new_flowers, target, full, partial),
result
);
}
}
// Accepted solution for LeetCode #2234: Maximum Total Beauty of the Gardens
function maximumBeauty(
flowers: number[],
newFlowers: number,
target: number,
full: number,
partial: number,
): number {
flowers.sort((a, b) => a - b);
const n = flowers.length;
const s: number[] = Array(n + 1).fill(0);
for (let i = 1; i <= n; i++) {
s[i] = s[i - 1] + flowers[i - 1];
}
let x = flowers.filter(f => f >= target).length;
let ans = 0;
for (; x <= n; ++x) {
newFlowers -= x === 0 ? 0 : Math.max(target - flowers[n - x], 0);
if (newFlowers < 0) {
break;
}
let l = 0;
let r = n - x - 1;
while (l < r) {
const mid = (l + r + 1) >> 1;
if (flowers[mid] * (mid + 1) - s[mid + 1] <= newFlowers) {
l = mid;
} else {
r = mid - 1;
}
}
let y = 0;
if (r !== -1) {
const cost = flowers[l] * (l + 1) - s[l + 1];
y = Math.min(flowers[l] + Math.floor((newFlowers - cost) / (l + 1)), target - 1);
}
ans = Math.max(ans, x * full + y * partial);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.