Given the constraints of the problem,
you easily code up a solution where you iterate through baskets for each fruit inside of fruits
and check if there remains any baskets that can fit the current fruit.
If this basket exists, then we can just set the current basket to 0 so that it cannot be used by future fruits.
code
class Solution {
public:
int numOfUnplacedFruits(vector<int>& fruits, vector<int>& baskets) {
int ans = 0;
for (int& fruit : fruits) {
int stored = false;
for (int& basket : baskets) {
if (basket >= fruit) {
basket = 0;
stored = true;
break;
}
}
if (!stored) {
++ans;
}
}
return ans;
}
};
complexity
- Time:
O(n * m)wherenis the number of elements insidefruitsandmis the number of elements insidebaskets- Space:
O(1)
time taken
- 3 minutes and 44 seconds