Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Last active September 4, 2025 18:49
Show Gist options
  • Save tatsuyax25/807284c42087e7996b0c50ff39a07ed3 to your computer and use it in GitHub Desktop.
Save tatsuyax25/807284c42087e7996b0c50ff39a07ed3 to your computer and use it in GitHub Desktop.
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
/**
* @param {number[]} fruits
* @param {number[]} baskets
* @return {number}
*/
var numOfUnplacedFruits = function(fruits, baskets) {
// Track basket usage using a Set to store used indices
const usedBaskets = new Set();
// Track number of unplaced fruits
let unplacedCount = 0;
// Loop through each fruit in order
for (let i = 0; i < fruits.length; i++) {
const quantity = fruits[i];
let placed = false;
// Find the first unused basket that can hold the fruit
for (let j = 0; j < baskets.length; j++) {
if (!usedBaskets.has(j) && baskets[j] >= quantity) {
usedBaskets.add(j); // Mark this basket as used
placed = true;
break; // Move to the next fruit
}
}
// If no basket was found, increment the unplaced count
if (!placed) {
unplacedCount++;
}
}
return unplacedCount;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment