We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2284805 commit d4ba436Copy full SHA for d4ba436
problem-of-the-day/March/26_minOpsToMakeAUniValueGrid.cpp
@@ -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