-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
37 lines (30 loc) · 922 Bytes
/
Copy pathsolution.java
File metadata and controls
37 lines (30 loc) · 922 Bytes
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
class Solution {
public int rowWithMax1s(int arr[][]) {
int n = arr.length;
int m = arr[0].length;
int maxOnes = 0;
int answer = -1;
for (int i = 0; i < n; i++) {
int low = 0, high = m - 1;
int firstOne = -1;
// Binary search in current row
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[i][mid] == 1) {
firstOne = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
if (firstOne != -1) {
int onesCount = m - firstOne;
if (onesCount > maxOnes) {
maxOnes = onesCount;
answer = i;
}
}
}
return answer;
}
}