-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2033.cpp
More file actions
35 lines (35 loc) · 1.05 KB
/
2033.cpp
File metadata and controls
35 lines (35 loc) · 1.05 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
class Solution {
public:
int minOperations(vector<vector<int>>& grid, int x) {
map<int, int> mp;
int m = grid.size();
int n = grid[0].size();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
mp[grid[i][j]]++;
}
}
int current = (*mp.begin()).first;
int op = 0;
vector<pair<int, int>> nums;
vector<int> prefixSum(m * n + 1, 0);
int index = 1;
for (auto& [num, cnt] : mp) {
int diff = num - current;
if (diff % x != 0) return -1;
op += diff / x * cnt;
nums.push_back({num, cnt});
prefixSum[index] = prefixSum[index - 1] + cnt;
index++;
}
int res = op;
int k = nums.size();
for (int i = 1; i < k; ++i) {
int diff = nums[i].first - nums[i - 1].first;
op += diff / x * prefixSum[i];
op -= diff / x * (prefixSum[k] - prefixSum[i]);
res = min(op, res);
}
return res;
}
};