




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.
1#include <stdio.h>
2#include <limits.h>
3#include <stdlib.h>
4
5int compare(const void *a, const void *b) {
6    return (*(int*)a - *(int*)b);
7}
8
9long long calcCost(int *nums, int *cost, int n, int target) {
10    long long totalCost = 0;
11    for (int i = 0; i < n; i++) {
12        totalCost += abs(nums[i] - target) * (long long)cost[i];
13    }
14    return totalCost;
15}
16
17long long minCost(int* nums, int numsSize, int* cost, int costSize) {
18    int minNum = INT_MAX, maxNum = INT_MIN;
19    for (int i = 0; i < numsSize; i++) {
20        if (nums[i] < minNum) minNum = nums[i];
21        if (nums[i] > maxNum) maxNum = nums[i];
22    }
23    
24    long long result = LLONG_MAX;
25    while (minNum <= maxNum) {
26        int mid = minNum + (maxNum - minNum) / 2;
27        long long cost1 = calcCost(nums, cost, numsSize, mid);
28        long long cost2 = calcCost(nums, cost, numsSize, mid + 1);
29        result = cost1 < cost2 ? cost1 : cost2;
30        if (cost1 < cost2) {
31            maxNum = mid - 1;
32        } else {
33            minNum = mid + 1;
34        }
35    }
36    return result;
37}
38
39int main() {
40    int nums[] = {1, 3, 5, 2};
41    int cost[] = {2, 3, 1, 14};
42    int n = sizeof(nums) / sizeof(nums[0]);
43    printf("Minimum cost: %lld\n", minCost(nums, n, cost, n));
44    return 0;
45}This C solution first finds the minimum and maximum numbers in the nums array to set the bounds for the binary search. It searches for the target value using binary search that minimizes the total cost to make all elements in nums equal to the target.
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.