forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-array-non-decreasing-or-non-increasing.cpp
49 lines (45 loc) · 1.55 KB
/
make-array-non-decreasing-or-non-increasing.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
// Time: O(nlogn)
// Space: O(n)
// greedy, heap
class Solution {
public:
int convertArray(vector<int>& nums) {
const auto& f = [](const auto& begin, const auto& end) {
int result = 0;
priority_queue<int> max_heap;
for (auto it = begin; it != end; ++it) {
if (!empty(max_heap) && *it < max_heap.top()) {
result += max_heap.top() - *it; max_heap.pop();
max_heap.emplace(*it);
}
max_heap.emplace(*it);
}
return result;
};
return min(f(cbegin(nums), cend(nums)), f(crbegin(nums), crend(nums)));
}
};
// Time: O(n^2)
// Space: O(n)
// dp
class Solution2 {
public:
int convertArray(vector<int>& nums) {
unordered_set<int> nums_set(cbegin(nums), cend(nums));
vector<int> vals(cbegin(nums_set), cend(nums_set));
sort(begin(vals), end(vals));
const auto& f = [&](const auto& begin, const auto& end) {
int result = 0;
unordered_map<int, int> dp; // dp[i]: min(cnt(j) for j in vals if j <= i)
for (auto it = begin; it != end; ++it) {
int prev = -1;
for (const auto& i : vals) {
dp[i] = (prev != -1) ? min(dp[i] + abs(i - *it), dp[prev]) : dp[i] + abs(i - *it);
prev = i;
}
}
return dp[vals.back()];
};
return min(f(cbegin(nums), cend(nums)), f(crbegin(nums), crend(nums)));
}
};