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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given two arrays of integers, fruits and baskets, each of length n, where fruits[i] represents the quantity of the ith type of fruit, and baskets[j] represents the capacity of the jth basket.
From left to right, place the fruits according to these rules:
Return the number of fruit types that remain unplaced after all possible allocations are made.
Example 1:
Input: fruits = [4,2,5], baskets = [3,5,4]
Output: 1
Explanation:
fruits[0] = 4 is placed in baskets[1] = 5.fruits[1] = 2 is placed in baskets[0] = 3.fruits[2] = 5 cannot be placed in baskets[2] = 4.Since one fruit type remains unplaced, we return 1.
Example 2:
Input: fruits = [3,6,1], baskets = [6,4,7]
Output: 0
Explanation:
fruits[0] = 3 is placed in baskets[0] = 6.fruits[1] = 6 cannot be placed in baskets[1] = 4 (insufficient capacity) but can be placed in the next available basket, baskets[2] = 7.fruits[2] = 1 is placed in baskets[1] = 4.Since all fruits are successfully placed, we return 0.
Constraints:
n == fruits.length == baskets.length1 <= n <= 1001 <= fruits[i], baskets[i] <= 1000Problem summary: You are given two arrays of integers, fruits and baskets, each of length n, where fruits[i] represents the quantity of the ith type of fruit, and baskets[j] represents the capacity of the jth basket. From left to right, place the fruits according to these rules: Each fruit type must be placed in the leftmost available basket with a capacity greater than or equal to the quantity of that fruit type. Each basket can hold only one type of fruit. If a fruit type cannot be placed in any basket, it remains unplaced. Return the number of fruit types that remain unplaced after all possible allocations are made.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Segment Tree
[4,2,5] [3,5,4]
[3,6,1] [6,4,7]
fruit-into-baskets)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3477: Fruits Into Baskets II
class Solution {
public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
int n = fruits.length;
boolean[] vis = new boolean[n];
int ans = n;
for (int x : fruits) {
for (int i = 0; i < n; ++i) {
if (baskets[i] >= x && !vis[i]) {
vis[i] = true;
--ans;
break;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #3477: Fruits Into Baskets II
func numOfUnplacedFruits(fruits []int, baskets []int) int {
n := len(fruits)
ans := n
vis := make([]bool, n)
for _, x := range fruits {
for i, y := range baskets {
if y >= x && !vis[i] {
vis[i] = true
ans--
break
}
}
}
return ans
}
# Accepted solution for LeetCode #3477: Fruits Into Baskets II
class Solution:
def numOfUnplacedFruits(self, fruits: List[int], baskets: List[int]) -> int:
n = len(fruits)
vis = [False] * n
ans = n
for x in fruits:
for i, y in enumerate(baskets):
if y >= x and not vis[i]:
vis[i] = True
ans -= 1
break
return ans
// Accepted solution for LeetCode #3477: Fruits Into Baskets II
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3477: Fruits Into Baskets II
// class Solution {
// public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
// int n = fruits.length;
// boolean[] vis = new boolean[n];
// int ans = n;
// for (int x : fruits) {
// for (int i = 0; i < n; ++i) {
// if (baskets[i] >= x && !vis[i]) {
// vis[i] = true;
// --ans;
// break;
// }
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3477: Fruits Into Baskets II
function numOfUnplacedFruits(fruits: number[], baskets: number[]): number {
const n = fruits.length;
const vis: boolean[] = Array(n).fill(false);
let ans = n;
for (const x of fruits) {
for (let i = 0; i < n; ++i) {
if (baskets[i] >= x && !vis[i]) {
vis[i] = true;
--ans;
break;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
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: 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.