forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-cost-to-make-array-equal.cpp
115 lines (108 loc) · 3.46 KB
/
minimum-cost-to-make-array-equal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Time: O(nlogn)
// Space: O(n)
// math, binary search
class Solution {
public:
long long minCost(vector<int>& nums, vector<int>& cost) {
const auto& f = [&](int x) {
int64_t result = 0;
for (int i = 0; i < size(nums); ++i) {
result += static_cast<int64_t>(abs(nums[i] - x)) * cost[i];
}
return result;
};
const auto& check = [&](int x, int64_t t) {
int64_t cnt = 0;
for (int i = 0; i < size(nums); ++i) {
if (nums[i] <= x) {
cnt += cost[i];
}
}
return cnt >= t;
};
vector<int> idxs(size(nums));
iota(begin(idxs), end(idxs), 0);
sort(begin(idxs), end(idxs), [&](const auto& a, const auto& b) {
return nums[a] < nums[b];
});
int left = 0, right = size(idxs) - 1;
const int64_t total = accumulate(cbegin(cost), cend(cost), 0ll);
const int64_t median = (total + 1) / 2;
while (left <= right) {
const int mid = left + (right - left) / 2;
if (check(nums[idxs[mid]], median)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return f(nums[idxs[left]]);
}
};
// Time: O(nlogn)
// Space: O(n)
// binary search
class Solution2 {
public:
long long minCost(vector<int>& nums, vector<int>& cost) {
const auto& f = [&](int x) {
int64_t result = 0;
for (int i = 0; i < size(nums); ++i) {
result += static_cast<int64_t>(abs(nums[i] - x)) * cost[i];
}
return result;
};
vector<int> idxs(size(nums));
iota(begin(idxs), end(idxs), 0);
sort(begin(idxs), end(idxs), [&](const auto& a, const auto& b) {
return nums[a] < nums[b];
});
const auto& check = [&](int x) {
return x + 1 == size(idxs) || f(nums[idxs[x]]) < f(nums[idxs[x + 1]]);
};
int left = 0, right = size(idxs) - 1;
while (left <= right) {
const int mid = left + (right - left) / 2;
if (check(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return f(nums[idxs[left]]);
}
};
// Time: O(nlogn)
// Space: O(n)
// prefix sum
class Solution3 {
public:
long long minCost(vector<int>& nums, vector<int>& cost) {
vector<int> idxs(size(nums));
iota(begin(idxs), end(idxs), 0);
sort(begin(idxs), end(idxs), [&](const auto& a, const auto& b) {
return nums[a] < nums[b];
});
vector<int64_t> prefix(size(cost) + 1);
int64_t left = 0;
for (int i = 0; i < size(cost); ++i) {
if (i - 1 >= 0) {
left += prefix[i] * (nums[idxs[i]] - nums[idxs[i - 1]]);
}
prefix[i + 1] = prefix[i] + cost[idxs[i]];
}
int64_t result = numeric_limits<int64_t>::max();
int64_t suffix = 0, right = 0;
for (int i = size(cost) - 1; i >= 0; --i) {
if (i + 1 < size(idxs)) {
right += suffix * (nums[idxs[i + 1]] - nums[idxs[i]]);
}
result = min(result, left + right);
if (i - 1 >= 0) {
left -= prefix[i] * (nums[idxs[i]] - nums[idxs[i - 1]]);
}
suffix += cost[idxs[i]];
}
return result;
}
};