-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2033.cpp
More file actions
25 lines (21 loc) · 744 Bytes
/
Copy path2033.cpp
File metadata and controls
25 lines (21 loc) · 744 Bytes
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
class Solution {
public:
int minOperations(const vector<vector<int>> &grid, int x) const {
static int dp[100001];
vector<int> vec;
for (const auto &row : grid)
vec.insert(end(vec), begin(row), end(row));
sort(begin(vec), end(vec));
for (int i = 0, acc = 0; i < size(vec) - 1; i++) {
const int crnt = vec[i + 1] - vec[i];
if (crnt % x) return -1;
dp[i + 1] = acc += (i + 1) * crnt;
}
int res = INT_MAX;
for (int i = size(vec) - 1, acc = 0; i > 0; i--) {
acc += (size(vec) - i) * (vec[i] - vec[i - 1]);
res = min(res, acc + dp[i - 1]);
}
return res != INT_MAX ? res / x : 0;
}
};