-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
49 lines (42 loc) · 1.09 KB
/
Copy pathsolution.cpp
File metadata and controls
49 lines (42 loc) · 1.09 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
class Solution
{
public:
int rowWithMax1s(vector<vector<int>> &arr)
{
int n = arr.size();
int m = arr[0].size();
int maxOnes = 0;
int answer = -1;
// Traverse each row
for (int i = 0; i < n; i++)
{
int low = 0, high = m - 1;
int firstOne = -1;
// Binary search for first 1
while (low <= high)
{
int mid = low + (high - low) / 2;
if (arr[i][mid] == 1)
{
firstOne = mid;
high = mid - 1;
}
else
{
low = mid + 1;
}
}
// If row contains at least one 1
if (firstOne != -1)
{
int onesCount = m - firstOne;
if (onesCount > maxOnes)
{
maxOnes = onesCount;
answer = i;
}
}
}
return answer;
}
};