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.
Input: fruits =[3,6,1], baskets =[6,4,7]Output: 0Explanation:
*`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 return0.
To efficiently find the leftmost available basket for each fruit, we can use an ordered set or a balanced BST to keep track of available baskets and their indices. This allows us to quickly find the smallest index basket with enough capacity for each fruit, and remove it once used.
#include<set>classSolution {
public:int unplacedFruits(vector<int>& fruits, vector<int>& baskets) {
set<pair<int, int>> s;
for (int i =0; i < baskets.size(); ++i) s.insert({baskets[i], i});
int ans =0;
for (int f : fruits) {
auto it = s.lower_bound({f, -1});
if (it == s.end()) ans++;
else s.erase(it);
}
return ans;
}
};
1
// For Go, sort baskets and use a used array as in the simple greedy, as Go has no built-in ordered set.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.TreeSet;
classSolution {
publicintunplacedFruits(int[] fruits, int[] baskets) {
TreeSet<int[]> set =new TreeSet<>((a, b) -> a[0]!= b[0]? a[0]- b[0] : a[1]- b[1]);
for (int i = 0; i < baskets.length; i++) set.add(newint[]{baskets[i], i});
int ans = 0;
for (int f : fruits) {
int[] key = {f, -1};
int[] found = set.ceiling(key);
if (found ==null) ans++;
else set.remove(found);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.TreeSet
classSolution {
fununplacedFruits(fruits: IntArray, baskets: IntArray): Int {
val set = TreeSet(compareBy<Pair<Int, Int>>({ it.first }, { it.second }))
for (i in baskets.indices) set.add(Pair(baskets[i], i))
var ans = 0for (f in fruits) {
val found = set.ceiling(Pair(f, -1))
if (found ==null) ans++elseset.remove(found)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
import bisect
classSolution:
defunplacedFruits(self, fruits: list[int], baskets: list[int]) -> int:
arr = sorted((b, i) for i, b in enumerate(baskets))
ans =0for f in fruits:
idx = bisect.bisect_left(arr, (f, -1))
if idx == len(arr):
ans +=1else:
arr.pop(idx)
return ans
1
// For Rust, use a BTreeSet or Vec with binary search for similar effect.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
unplacedFruits(fruits: number[], baskets: number[]):number {
constarr: [number, number][] =baskets.map((b, i) => [b, i]);
arr.sort((a, b) =>a[0] -b[0] ||a[1] -b[1]);
letans=0;
for (constfoffruits) {
letl=0, r=arr.length;
while (l<r) {
constm= (l+r) >>1;
if (arr[m][0] <f) l=m+1;
elser=m;
}
if (l===arr.length) ans++;
elsearr.splice(l, 1);
}
returnans;
}
}