




Sponsored
Sponsored
This approach involves treating the problem as a minimization problem where the minimum total cost is calculated by iterating over potential target values using binary search. At each step, the cost to make all elements equal to a proposed target is calculated, and the goal is to find the target that results in the minimal cost. By narrowing the range of potential target values using binary search, this approach efficiently finds the optimal target.
Time Complexity: O(n log(MAX_DIFF)), where MAX_DIFF is the range of possible numbers.
Space Complexity: O(1) since it only uses a fixed amount of extra space.
1import java.util.*;
2
3public class MinCostEqualArray {
4
5    public static long calculateCost(int[] nums, int[] cost, int target) {
6        long totalCost = 0;
7        for (int i = 0; i < nums.length; i++) {
8            totalCost += Math.abs(nums[i] - target) * (long)cost[i];
9        }
10        return totalCost;
11    }
12
13    public static long minCost(int[] nums, int[] cost) {
14        int minNum = Arrays.stream(nums).min().getAsInt();
15        int maxNum = Arrays.stream(nums).max().getAsInt();
16        long result = Long.MAX_VALUE;
17        while (minNum <= maxNum) {
18            int midNum = minNum + (maxNum - minNum) / 2;
19            long costMid = calculateCost(nums, cost, midNum);
20            long costMidPlusOne = calculateCost(nums, cost, midNum + 1);
21            result = Math.min(costMid, costMidPlusOne);
22            if (costMid < costMidPlusOne) {
23                maxNum = midNum - 1;
24            } else {
25                minNum = midNum + 1;
26            }
27        }
28        return result;
29    }
30
31    public static void main(String[] args) {
32        int[] nums = {1, 3, 5, 2};
33        int[] cost = {2, 3, 1, 14};
34        System.out.println("Minimum cost: " + minCost(nums, cost));
35    }
36}The Java solution leverages Java Streams to find the min and max values quickly for the binary search bounds. It then proceeds with a similar binary search method as the C/C++ implementations to find the minimum cost for an equalized array.
The main idea is to use the concept of a weighted median to find the optimal target, a value to which all elements should be equalized to minimize cost. The weighted median is the best choice for minimizing the cost because it balances out the costs by taking into account where the bulk of weights lie. By considering each element's weight (cost), the weighted median is a statistically optimal choice that minimizes the total cost for the adjustment.
Time Complexity: O(n log n) due to sorting.
Space Complexity: O(n) for auxiliary space used in sorting.
1def min_cost(nums, cost):
2    zipped_pairs = sorted(zip(    
This Python solution involves sorting an array of pairs [num, cost], aiming to identify the weighted median. We accumulate the weights and find a point where the accumulated weight is equal to or exceeds half the total weight, signifying the median. That value is used as the target, minimizing the total cost of modification via a single pass.