Program to find minimum costs needed to fill fruits in optimized way in Python

Suppose we have a list called fruits and two values k and cap. Each fruits[i] contains three values: [c, s, t], where fruit i costs c each, has size s, and there are t total fruits available. The k represents number of fruit baskets with capacity cap. We want to fill the fruit baskets with the following constraints in this order ?

  • Each basket can only hold same type fruits
  • Each basket should be as full as possible
  • Each basket should be as cheap as possible

We need to find the minimum cost required to fill as many baskets as possible.

Problem Understanding

If the input is fruits = [[5, 2, 3], [6, 3, 2], [2, 3, 2]], k = 2, cap = 4, then the output will be 12. This is because we can take two fruits of type 0 (cost 5, size 2 each) to fill the first basket completely (size 2+2=4) for cost 5+5=10. Then we use one fruit of type 2 (cost 2, size 3) for the second basket, costing 2 units. Total cost = 10 + 2 = 12.

Algorithm Steps

To solve this problem, we follow these steps ?

  • Create an options list to store all possible basket configurations
  • For each fruit type (c, s, t):
    • Calculate how many fruits can fit in one basket: fnum = min(cap // s, t)
    • Calculate how many such baskets we can make: bnum = t // fnum
    • Store the configuration as (remaining_capacity, basket_cost, basket_count)
    • Update remaining fruits count
  • Sort options by remaining capacity (prioritize fuller baskets)
  • Greedily fill baskets starting with the fullest and cheapest options

Implementation

def solve(fruits, k, cap):
    options = []
    
    # Generate all possible basket configurations
    for c, s, t in fruits:
        while t > 0:
            # Maximum fruits that can fit in one basket
            fnum = min(cap // s, t)
            if fnum == 0:
                break
            
            # Number of such baskets we can make
            bnum = t // fnum
            
            # Store: (remaining_capacity, basket_cost, basket_count)
            options.append((cap - fnum * s, fnum * c, bnum))
            t -= bnum * fnum
    
    # Sort by remaining capacity (fuller baskets first), then by cost
    ans = 0
    for left_cap, bcost, bnum in sorted(options):
        # Fill as many baskets as possible
        bfill = min(k, bnum)
        ans += bcost * bfill
        k -= bfill
        
        if k == 0:
            break
    
    return ans

# Test the function
fruits = [[5, 2, 3], [6, 3, 2], [2, 3, 2]]
k = 2
cap = 4
result = solve(fruits, k, cap)
print(f"Minimum cost to fill {k} baskets: {result}")
Minimum cost to fill 2 baskets: 12

How It Works

The algorithm works by:

  • Configuration Generation: For each fruit type, it calculates all possible ways to fill baskets
  • Greedy Selection: It sorts configurations by fullness (lower remaining capacity first) to prioritize fuller baskets
  • Cost Optimization: Among equally full baskets, it naturally selects cheaper options first due to sorting

Step-by-Step Trace

For our example with fruits = [[5, 2, 3], [6, 3, 2], [2, 3, 2]]:

  • Fruit 0: cost=5, size=2, count=3 ? Can fit 2 fruits per basket (size 4), make 1 basket, cost=10
  • Fruit 1: cost=6, size=3, count=2 ? Can fit 1 fruit per basket (size 3), make 2 baskets, cost=6 each
  • Fruit 2: cost=2, size=3, count=2 ? Can fit 1 fruit per basket (size 3), make 2 baskets, cost=2 each

Options sorted by remaining capacity: [(0, 10, 1), (1, 6, 2), (1, 2, 2)]

Fill baskets: First basket costs 10, second basket costs 2. Total = 12.

Conclusion

This greedy algorithm efficiently finds the minimum cost to fill fruit baskets by prioritizing fuller baskets first, then selecting the cheapest options. The time complexity is O(n log n) due to sorting the configuration options.

Updated on: 2026-03-26T17:16:53+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements