Skip to main content
Back to problems
Leetcode
Medium
Arrays
Greedy
Ordered Structures
Amazon
3479. Fruits Into Baskets III

Assign each fruit to the leftmost basket that can still hold it, then report how many fruits remain unplaced.

Acceptance 0%
Problem Statement

Fruits Into Baskets III

You are given two integer arrays, fruits and baskets, of the same length n.

Think of the i-th fruit as having size fruits[i], and the i-th basket as having capacity baskets[i].

You must process the fruits from left to right. For each fruit, place it into the leftmost basket that has not been used yet and whose capacity is at least the fruit size. If no such basket exists, that fruit cannot be placed.

Return the number of fruits that cannot be placed into any basket.

This is a greedy placement problem: every fruit should take the earliest feasible basket so that later fruits still have the best chance to fit.

Input Format

Input

  • fruits: an integer array of fruit sizes
  • baskets: an integer array of basket capacities

Both arrays have the same length n.

Output Format

Output

  • Return an integer representing how many fruits cannot be placed.

Constraints

  • 1 <= n <= $10^{5}$
  • 0 <= fruits[i], baskets[i] <= $10^{9}$
  • Each basket can be used at most once.
  • Process fruits in the given order.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.