-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram26.java
More file actions
78 lines (51 loc) · 2 KB
/
program26.java
File metadata and controls
78 lines (51 loc) · 2 KB
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
77
78
/*
2643. Row With Maximum Ones
Easy
149
4
Companies
Given a m x n binary matrix mat, find the 0-indexed position of the row that contains the maximum count of ones, and the number of ones in that row.
In case there are multiple rows that have the maximum count of ones, the row with the smallest row number should be selected.
Return an array containing the index of the row, and the number of ones in it.
Example 1:
Input: mat = [[0,1],[1,0]]
Output: [0,1]
Explanation: Both rows have the same number of 1's. So we return the index of the smaller row, 0, and the maximum count of ones (1). So, the answer is [0,1].
Example 2:
Input: mat = [[0,0,0],[0,1,1]]
Output: [1,2]
Explanation: The row indexed 1 has the maximum count of ones (2). So we return its index, 1, and the count. So, the answer is [1,2].
Example 3:
Input: mat = [[0,0],[1,1],[0,0]]
Output: [1,2]
Explanation: The row indexed 1 has the maximum count of ones (2). So the answer is [1,2].
*/
package LeetCode;
public class program26 {
static int[] rowAndMaximumOnes(int[][] mat) {
int[] res = new int[2];
for(int i=0;i<mat.length;i++){
int cnt=0;
for(int j=0;j<mat[0].length;j++){
if(mat[i][j]==1){
cnt++;
}
}
if(res[1]==cnt){
continue;
}
else if(cnt > res[1]){
res[0] = i;
res[1] = cnt;
}
}
return res;
}
public static void main(String[] args) {
int mat[][] = {{0,0},{1,1},{0,0}};
int a[] = rowAndMaximumOnes(mat);
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}