Fruits Into Baskets II

You are given two arrays of length `n`: `fruits[i]` is the quantity of fruit type `i`, and `baskets[j]` is the capacity of basket `j`. Going through the fruits from left to right, place each one in the leftmost basket that is still empty and has capacity at least that quantity. Each basket holds one fruit type. If no basket fits, the fruit stays unplaced. Return the number of unplaced fruit types.

30:00

Fruits Into Baskets II
easy
Free written solution
Topics
Companies

You are given two arrays of length n: fruits[i] is the quantity of fruit type i, and baskets[j] is the capacity of basket j. Going through the fruits from left to right, place each one in the leftmost basket that is still empty and has capacity at least that quantity. Each basket holds one fruit type. If no basket fits, the fruit stays unplaced. Return the number of unplaced fruit types.

Example 1:
Input: {"fruits":[4,2,5],"baskets":[3,5,4]}
Output: 1
Constraints:
  • n=fruits.length=baskets.lengthn = \text{fruits.length} = \text{baskets.length}

  • 1≤n≤1001 \leq n \leq 100

  • 1≤fruits[i],baskets[i]≤10001 \leq fruits[i], baskets[i] \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"fruits":[4,2,5],"baskets":[3,5,4]}

fruits (in arrival order)

4
2
5

baskets (height = capacity, the dashed line = current fruit size)

3

#0

5

#1

4

#2

3 fruit type(s) arrive in order and 3 empty baskets wait in a row. Each fruit takes the leftmost free basket that is big enough.

unplaced = 0
Variables
VariableValue
unplaced0
DepthFunction Call
Stack empty
0/11