Advertisement

Minimum Cost to Make Array Equal - LeetCode 2448 Solution

Minimum Cost to Make Array Equal - Complete Solution Guide

Minimum Cost to Make Array Equal is LeetCode problem 2448, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given two 0-indexed arrays nums and cost consisting each of n positive integers. You can do the following operation any number of times: Increase or decrease any element of the array nums by 1 . The cost of doing one operation on the i th element is cost[i] . Return the minimum total cost such that all the elements of the array nums become equal . Example 1: Input: nums = [1,3,5,2], cost = [2,3,1,14] Output: 8 Explanation: We can make all the elements equal to 2 in the following way: - I

Detailed Explanation

The problem asks us to find the minimum cost to make all elements of an array `nums` equal. We are given another array `cost`, where `cost[i]` represents the cost of increasing or decreasing `nums[i]` by 1. The goal is to determine the target value to which we should equalize all the elements of `nums` such that the total cost, calculated based on `cost`, is minimized. The inputs are two arrays: `nums` and `cost`, both of size `n`, containing positive integers. The output is a single integer representing the minimum total cost.

Solution Approach

The solution utilizes the concept of the weighted median to minimize the cost. First, pairs of (number, cost) are created and sorted by the number. Then, the algorithm iterates through these pairs, calculating the cumulative sum of the costs. The optimal target value is the number at which the cumulative cost reaches or exceeds half of the total cost sum. This is based on the property that the minimum cost is achieved when equalizing all numbers to the weighted median. Finally, the solution calculates the total cost of making all numbers equal to the target value.

Step-by-Step Algorithm

  1. Step 1: Create an array of pairs (num, cost) from the input arrays `nums` and `cost`.
  2. Step 2: Sort the array of pairs in ascending order based on the `num` value.
  3. Step 3: Calculate the total sum of all costs.
  4. Step 4: Iterate through the sorted pairs, calculating the cumulative cost.
  5. Step 5: During iteration, check if `cumulative_cost * 2 >= total_cost_sum`. If true, the current `num` is the weighted median (target number). Break the loop.
  6. Step 6: Calculate the total cost by summing the absolute differences between each number in the original `nums` array and the target number, multiplied by the corresponding cost from the `cost` array.
  7. Step 7: Return the calculated total cost.

Key Insights

  • Insight 1: The target value to which we should equalize the array elements is the weighted median of the `nums` array, with `cost` serving as the weights. This is because the cost function (sum of absolute differences multiplied by corresponding costs) is a convex function, and its minimum lies at the weighted median.
  • Insight 2: Sorting the `nums` array allows us to efficiently calculate the cumulative weighted sum, which is crucial for identifying the weighted median.
  • Insight 3: The total cost can exceed the range of `int`, requiring the use of `long` data type in languages like Java and C++ to prevent overflow.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Topics

This problem involves: Array, Binary Search, Greedy, Sorting, Prefix Sum.

Companies

Asked at: Cisco, HashedIn, J.P. Morgan.