-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3229.cpp
More file actions
41 lines (40 loc) · 1.35 KB
/
3229.cpp
File metadata and controls
41 lines (40 loc) · 1.35 KB
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
class Solution {
public:
long long minimumOperations(vector<int>& nums, vector<int>& target) {
long long res = 0;
int n = nums.size();
vector<int> differences(n, 0);
for (int i = 0; i < n; ++i) {
differences[i] = target[i] - nums[i];
}
int index = 0;
while (index < n) {
if (differences[index] < 0) {
int start = index;
while (index + 1 < n && differences[index + 1] < 0) index++;
// [start, index]
int current = 0;
for (int j = start; j <= index; ++j) {
if (differences[j] < current) {
res += current - differences[j];
}
current = differences[j];
}
}
else if (differences[index] > 0) {
int start = index;
while (index + 1 < n && differences[index + 1] > 0) index++;
// [start, index]
int current = 0;
for (int j = start; j <= index; ++j) {
if (differences[j] > current) {
res += differences[j] - current;
}
current = differences[j];
}
}
index++;
}
return res;
}
};