Fruits Into Baskets II

Fruits Into Baskets II

You are given two integer arrays, fruits and baskets, both of length n. Each fruits[i] represents the weight of the i-th fruit, and each baskets[j] represents the capacity of the j-th basket. You need to process the fruits in the order they appear in the input (from index 0 to n-1). For each fruit, place it in the leftmost available basket that has a capacity greater than or equal to the fruit's weight. Once a fruit is placed in a basket, that basket becomes unavailable for any subsequent fruits. Return the total number of fruits that cannot be placed in any basket. Constraints: n == fruits.length == baskets.length 1 \le n \le 10^5 1 \le fruits[i], baskets[j] \le 10^9
JavaSegment TreeBinary Search
00
Read

Fruits Into Baskets II

You are given two integer arrays, fruits and baskets, both of size n. The array fruits[i] represents the quantity of the i-th fruit, and baskets[j] represents the capacity of the j-th basket. You must process the fruits in the order they appear in the fruits array. For each fruit, you want to place it into the first available basket (the one with the smallest index) that has a capacity greater than or equal to the fruit's quantity. Once a fruit is placed in a basket, that basket becomes unavailable for any other fruits. If a fruit cannot fit into any available basket, it remains unplaced. Return the total number of fruits that could not be placed in any basket. Constraints: n == fruits.length == baskets.length 1 <= n <= 10^5 1 <= fruits[i], baskets[i] <= 10^9
JavaSegment Tree
00
Read
1
InterviewGPT

AI-powered tools to help you succeed in tech interviews — from resume to offer.

Interview Solver

  • Coding Puzzles
  • System Design
  • Behavioral Challenges
  • ML System Design
  • SQL Puzzles
  • FE System Design
Explore Solver

Question Bank

  • Coding Interview Questions
  • System Design Interview Questions
  • Behavioral Interview Questions
  • ML System Design Questions
  • SQL & Database Questions
  • FE System Design Questions
Explore Questions

Golden Blogs

  • Coding Solutions
  • System Design Guides
  • Behavioral Guides
  • ML System Design Guides
  • SQL Solutions
  • FE System Design Guides
Explore Blogs

Intervipedia

  • Coding Concepts
  • System Design Concepts
  • Behavioral Concepts
  • ML System Concepts
  • SQL Concepts
  • FE System Concepts
Explore Concepts

Application Tools

  • Self-Intro Generator

Company

  • Pricing
  • FAQ
  • About
  • Privacy Policy
  • Terms of Service

© 2026 InterviewGPT Inc. All rights reserved.

All systems operationalUS-East

Made with ♥ for developers