LeetCode Problem

How to Solve Fruits Into Baskets III

Start by sorting baskets by capacity and index, then use binary search over the maximum number of fruits that can fit. Track unplaced fruit types using a segment tree or ordered set to handle allocations efficiently. This approach ensures O(n log n) time while maintaining correct placement order and counting leftover fruit types accurately.

GhostInterview Help

Need help with Fruits Into Baskets III without spending extra time grinding it?

GhostInterview can read Fruits Into Baskets III from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #3479Binary search over the valid answer spaceReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Binary search over the valid answer space
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

Start by sorting baskets by capacity and index, then use binary search over the maximum number of fruits that can fit. Track unplaced fruit types using a segment tree or ordered set to handle allocations efficiently. This approach ensures O(n log n) time while maintaining correct placement order and counting leftover fruit types accurately.

Problem Statement

You are given two arrays, fruits and baskets, each of length n. fruits[i] indicates the quantity of the ith fruit type, while baskets[j] shows the capacity of the jth basket. From left to right, attempt to place each fruit type into the baskets according to their capacities, ensuring no basket exceeds its limit.

Return the number of fruit types that remain unplaced after attempting all allocations. For example, given fruits = [4,2,5] and baskets = [3,5,4], one fruit type cannot be placed, so the output is 1. Consider optimizing the process using binary search over the valid allocation space to determine feasible placement efficiently.

Examples

Example 1

Input: fruits = [4,2,5], baskets = [3,5,4]

Output: 1

Since one fruit type remains unplaced, we return 1.

Example 2

Input: fruits = [3,6,1], baskets = [6,4,7]

Output: 0

Since all fruits are successfully placed, we return 0.

Constraints

  • n == fruits.length == baskets.length
  • 1 <= n <= 105
  • 1 <= fruits[i], baskets[i] <= 109

Solution Approach

Sort and Prepare Baskets

Sort baskets by their capacity and original index to streamline allocation. This ordering helps when applying binary search and segment trees for placement verification.

Binary Search Over Maximum Placeable Fruits

Use binary search to identify the largest number of fruits that can be allocated without violating basket capacities. Check each candidate value using the segment tree to track remaining space efficiently.

Count Unplaced Fruit Types

After verifying allocations, count fruit types that could not fit into any basket. Maintain an ordered set or segment tree to quickly query available capacities and update remaining baskets during the allocation process.

Complexity Analysis

MetricValue
TimeO(n \log n)
SpaceO(n)

Time complexity is O(n log n) due to sorting and binary search across n fruit types. Space complexity is O(n) for segment tree or ordered set storage used to track basket capacities and remaining fruits.

What Interviewers Usually Probe

  • Sorting baskets hints at using structured allocation and binary search validation.
  • Ask about handling unplaced fruits efficiently, testing segment tree or ordered set use.
  • Check candidate maximum fruit placement with binary search over feasible answers.

Common Pitfalls or Variants

Common pitfalls

  • Failing to sort baskets before binary search can lead to incorrect placements.
  • Not updating basket capacities after each allocation causes overcounting or misplacement.
  • Confusing array indices when combining original positions with sorted capacities.

Follow-up variants

  • Allow partial placement of a fruit type, requiring tracking leftover quantities per basket.
  • Maximize total fruits placed rather than minimizing unplaced types.
  • Change pattern to allow multiple fruit types per basket with combined capacities.

How GhostInterview Helps

  • GhostInterview suggests sorting baskets and tracking capacities to quickly test allocations.
  • It models the binary search over valid answer space, highlighting feasible placements for interview scenarios.
  • Provides step-by-step counting of unplaced fruit types with clear visualization of segment tree or ordered set updates.

Topic Pages

FAQ

What is the main strategy for Fruits Into Baskets III?

Use binary search over the maximum placeable fruits, sorting baskets and checking allocations efficiently with a segment tree or ordered set.

Can baskets have the same capacity?

Yes, baskets can share capacities, so sorting by capacity and index ensures correct allocation order.

Why not just fill baskets greedily from left to right?

Greedy filling can fail to minimize unplaced fruit types; binary search over allocation space guarantees optimal counting.

How does the segment tree help in this problem?

Segment tree allows querying and updating available basket capacities quickly during allocation checks in binary search.

What interview pattern does this problem demonstrate?

It demonstrates binary search over a valid answer space, combined with array sorting and segment tree updates for feasibility checks.

GhostInterview Solver

Need direct help with Fruits Into Baskets III instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Fruits Into Baskets III from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.