-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathLargest Row or Column
36 lines (36 loc) · 1.09 KB
/
Largest Row or Column
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
public class Solution {
public static void findLargest(int mat[][]){
boolean isRow = true;
int largestSum = Integer.MIN_VALUE;
int num = 0; int nRows = mat.length;
if (nRows == 0) {
System.out.println("row " + num + " " + largestSum);
return;
}
int mCols = mat[0].length;
for(int i = 0; i < nRows;i++){
int rowSum = 0;
for(int j = 0; j < mCols; j++){
rowSum += mat[i][j];
}
if(rowSum > largestSum){
largestSum = rowSum; num = i;
}
}
for(int j = 0; j < mCols; j++){
int colSum = 0;
for(int i = 0; i < nRows; i++){
colSum += mat[i][j];
}
if(colSum > largestSum){
largestSum = colSum; num = j; isRow = false;
}
}
if(isRow){
System.out.println("row " + num + " " + largestSum);
}
else{
System.out.println("column " + num + " " + largestSum);
}
}
}