3477: Fruits-Into-Baskets-II
Easy


table of contents

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 taken