Skip to content

Commit d4ba436

Browse files
Day 26 : Min Ops to make a uni value grid
1 parent 2284805 commit d4ba436

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int minOperations(vector<vector<int>>& grid, int x) {
4+
int n = grid.size();
5+
int m = grid[0].size();
6+
7+
vector<int> vec;
8+
for(auto &row : grid){
9+
for(int &col : row){
10+
vec.push_back(col);
11+
}
12+
}
13+
14+
sort(begin(vec), end(vec));
15+
16+
int len = vec.size();
17+
int target;
18+
if(len % 2 == 0) target = vec[len/2];
19+
else target = vec[(len + 1)/2 - 1];
20+
21+
int ops = 0;
22+
for(int &i : vec){
23+
bool canExist = (abs(target - i) % x == 0);
24+
if(!canExist) return -1;
25+
26+
ops += (abs(target - i)/x);
27+
}
28+
29+
return ops;
30+
}
31+
};

0 commit comments

Comments
 (0)