Skip to content

Commit 003643f

Browse files
authored
Create 2033. Minimum Operations to Make a Uni-Value Grid (#752)
2 parents 23f9961 + b001ad8 commit 003643f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
int minOperations(vector<vector<int>>& grid, int x) {
4+
int rem=grid[0][0]%x;
5+
int n=grid.size();
6+
int m=grid[0].size();
7+
vector<int>r;
8+
for (int i=0;i<n;i++){
9+
for (int j=0;j<m;j++){
10+
if (grid[i][j]%x!=rem){
11+
return -1;
12+
} else{
13+
r.push_back(grid[i][j]);
14+
}
15+
}
16+
}
17+
sort(r.begin(),r.end());
18+
int s=m*n;
19+
int ind=s/2;
20+
int eq=r[ind];
21+
int c=0;
22+
for (int i=0;i<s;i++){
23+
if (eq!=r[i]){
24+
c+=abs(r[i]-eq)/x;
25+
}
26+
}
27+
return c;
28+
29+
}
30+
};

0 commit comments

Comments
 (0)