-
Notifications
You must be signed in to change notification settings - Fork 0
/
1887.reduction-operations-to-make-the-array-elements-equal.java
76 lines (55 loc) · 1.82 KB
/
1887.reduction-operations-to-make-the-array-elements-equal.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Solution {
public boolean findRotation(int[][] mat, int[][] target) {
int n = mat.length;
int m = mat[0].length;
int[] rowsMat = getRows(mat);
int[] colsMat = getCols(mat);
int[] rowsTar = getRows(target);
int[] colsTar = getCols(target);
// System.out.println(Arrays.toString(rowsMat)+" "+Arrays.toString(colsMat) +" "+Arrays.toString(rowsTar)+" "+Arrays.toString(colsTar));
return (check(rowsMat, rowsTar) && check(colsMat, colsTar)) || (check(rowsMat, colsTar) && check(colsMat , rowsTar));
}
public int[] getRows(int[][] mat){
int n = mat.length;
int m = mat[0].length;
int[] rows = new int[n];
// int[] cols = new int[m];
for(int i=0;i<n;i++){
int sum = 0;
for(int j=0;j<m;j++)
sum += mat[i][j];
rows[i] =sum;
}
return rows;
}
public int[] getCols(int[][] mat){
int n = mat.length;
int m = mat[0].length;
// int[] rows = new int[n];
int[] cols = new int[m];
for(int i=0;i<m;i++){
int sum = 0;
for(int j=0;j<n;j++)
sum += mat[j][i];
cols[i] =sum;
}
return cols;
}
public boolean check(int[] rowsMat, int[] rowsTar){
int n= rowsMat.length;
int i=0;
for(i=0;i<n;i++){
if(rowsMat[i] != rowsTar[i])
break;
}
if(i != n){
for(i=n-1;i>=0;i--){
if(rowsMat[i] != rowsTar[n-i-1])
break;
}
if(i != -1)
return false;
}
return true;
}
}