-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15_4.java
52 lines (39 loc) · 1.02 KB
/
day15_4.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
//Given a boolean 2D array, where each row is sorted. Find the row with the maximum number of 1s.
//User function Template for Java
class Sol
{
public static int maxOnes (int Mat[][], int N, int M)
{
// your code here
int index = -1;
HashMap<Integer,Integer> map = new HashMap<>();
for(int i=0; i<N; i++)
{
int ones =0;
for(int j=0; j<M; j++)
{
if(Mat[i][j]==1)
{
ones++;
}
}
map.put(i,ones);
}
int count =-1;
for(Map.Entry<Integer,Integer> entry: map.entrySet())
{
if(entry.getValue()>count)
{
count = entry.getValue();
}
}
for(int i=0; i<N ; i++)
{
if(map.get(i)==count)
{
return i;
}
}
return index;
}
}