Program to find minimum cost to reduce a list into one integer in Python

Suppose we have a list of numbers called nums. We can reduce the length of nums by taking any two numbers, removing them, and appending their sum at the end. The cost of doing this operation is the sum of the two integers we removed. We have to find the minimum total cost of reducing nums to one integer.

So, if the input is like nums = [2, 3, 4, 5, 6], then the output will be 45. We take 2 and 3 then remove to get [4, 5, 6, 5], then we take 4 and 5 then remove to get [6, 5, 9], then take 6 and 5, then remove them and we get [9, 11], and finally remove 9 and 11, we will get [20]. So the total cost is 5 + 9 + 11 + 20 = 45.

Algorithm

To solve this, we will follow these steps ?

  • Make a min-heap using elements present in nums
  • Initialize ans = 0
  • While size of nums >= 2, do
    • a = pop smallest element from heap
    • b = pop smallest element from heap
    • ans = ans + a + b
    • Insert a+b back into heap
  • Return ans

Why Use a Min-Heap?

The key insight is that we should always combine the two smallest numbers first. This greedy approach minimizes the total cost because smaller numbers contribute to fewer subsequent operations.

Implementation

import heapq

class Solution:
    def solve(self, nums):
        # Create a min-heap from the list
        heapq.heapify(nums)
        total_cost = 0
        
        while len(nums) >= 2:
            # Pop two smallest elements
            first = heapq.heappop(nums)
            second = heapq.heappop(nums)
            
            # Add their sum to total cost
            cost = first + second
            total_cost += cost
            
            # Push the sum back to heap
            heapq.heappush(nums, cost)
        
        return total_cost

# Test the solution
solution = Solution()
nums = [2, 3, 4, 5, 6]
result = solution.solve(nums)
print(f"Minimum cost: {result}")
Minimum cost: 45

Step-by-Step Execution

Let's trace through the algorithm with nums = [2, 3, 4, 5, 6] ?

import heapq

def solve_with_trace(nums):
    heapq.heapify(nums)
    total_cost = 0
    step = 1
    
    print(f"Initial heap: {nums}")
    
    while len(nums) >= 2:
        first = heapq.heappop(nums)
        second = heapq.heappop(nums)
        cost = first + second
        total_cost += cost
        heapq.heappush(nums, cost)
        
        print(f"Step {step}: Remove {first} and {second}, cost = {cost}, heap = {nums}")
        step += 1
    
    print(f"Total minimum cost: {total_cost}")
    return total_cost

# Trace execution
nums = [2, 3, 4, 5, 6]
solve_with_trace(nums)
Initial heap: [2, 3, 4, 5, 6]
Step 1: Remove 2 and 3, cost = 5, heap = [4, 5, 5, 6]
Step 2: Remove 4 and 5, cost = 9, heap = [5, 6, 9]
Step 3: Remove 5 and 6, cost = 11, heap = [9, 11]
Step 4: Remove 9 and 11, cost = 20, heap = [20]
Total minimum cost: 45

Time and Space Complexity

  • Time Complexity: O(n log n), where n is the length of nums. Each heap operation takes O(log n) time and we perform n-1 iterations.
  • Space Complexity: O(1) extra space, as we modify the input list in-place to create the heap.

Conclusion

The minimum cost reduction problem is solved optimally using a greedy approach with a min-heap. Always combining the two smallest numbers ensures the minimum total cost, as demonstrated by the step-by-step execution showing a cost of 45 for the input [2, 3, 4, 5, 6].

Updated on: 2026-03-25T11:29:29+05:30

881 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements