Last active
September 4, 2025 18:49
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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