DDSA Solutions

3477. Fruits Into Baskets II

Time: O(n log n)
Space: O(1)

Problem Overview

Fruits Into Baskets II (Medium) asks you to solve a structured algorithmic task. This is a common Array / Binary Search pattern in coding interviews. Greedy; sort baskets; for each fruit find smallest capable basket using binary search.

A full step-by-step explanation is being added. See the study guide for pattern-based practice.

Approach

Greedy; sort baskets; for each fruit find smallest capable basket using binary search.

Related patterns: Array, Binary Search, Sliding Window

3477.cs
C#
// Approach: Greedy; sort baskets; for each fruit find smallest capable basket using binary search.
// Time: O(n log n) Space: O(1)

public class Solution
{
    public int NumOfUnplacedFruits(int[] fruits, int[] baskets)
    {
        int n = fruits.Length; // Number of fruits
        bool[] visited = new bool[n]; // Array to track used baskets
        int unplacedFruits = n; // Initialize all fruits as unplaced

        // Loop through each fruit
        foreach (int fruit in fruits)
        {
            // Try to place each fruit in an available basket
            for (int i = 0; i < n; ++i)
            {
                // Check if the current basket can hold the fruit
                // and if it hasn't been used yet
                if (baskets[i] >= fruit && !visited[i])
                {
                    visited[i] = true; // Mark the basket as used
                    --unplacedFruits; // Decrease count of unplaced fruits
                    break; // Move to the next fruit
                }
            }
        }
        return unplacedFruits; // Return the count of unplaced fruits
    }
}
Was this solution helpful?

Related Problems