给定n * n阶的二进制矩阵(仅包含0和1)。所有行均已排序,我们需要找到最大为1s的行号。还要在该行中找到数字1。
注意:如果是平局,请打印较小的行号。
例子 :
Input : mat[3][3] = {0, 0, 1,
0, 1, 1,
0, 0, 0}
Output : Row number = 2, MaxCount = 2
Input : mat[3][3] = {1, 1, 1,
1, 1, 1,
0, 0, 0}
Output : Row number = 1, MaxCount = 3
基本方法:遍历整个矩阵,并为每行找到1的数目,并在所有行中不断更新最大数目为1的行号。此方法将导致O(n ^ 2)时间复杂度。
更好的方法:如果尝试应用二进制搜索来查找每行中第一个1的位置,并且每行都按排序顺序从每行中找到1的数量,则我们可以做得更好。这将导致O(nlogn)时间复杂度。
高效的方法:从索引(1,n)的左上角开始,尝试向左移动直到到达该行(第j列)的最后1,现在如果我们向左移动到该行,我们将找到0,因此切换到下方的行,具有相同的列。现在,如果第j个元素(如果1)尝试向左移动,则您的位置将再次位于第二行(2,j),直到找到最后1个;否则,如果第j个元素为0,则在第二行中转到下一行。因此,最后要说的是,如果您位于第ith行和第j列中的任何一个,即该行右边的最后1个索引,则递增i。因此,现在如果Aij = 0再次增加i,否则继续减少j,直到在该特定行中找到最后1个为止。
样本插图:
算法 :
for (int i=0, j=n-1; iC++
// CPP program to find row with maximum 1
// in row sorted binary matrix
#include
#define N 4
using namespace std;
// function for finding row with maximum 1
void findMax (int arr[][N])
{
int row = 0, i, j;
for (i=0, j=N-1; i= 0)
{
row = i;
j--;
}
}
cout << "Row number = " << row+1;
cout << ", MaxCount = " << N-1-j;
}
// driver program
int main()
{
int arr[N][N] = {0, 0, 0, 1,
0, 0, 0, 1,
0, 0, 0, 0,
0, 1, 1, 1};
findMax(arr);
return 0;
}
Java
// Java program to find row with maximum 1
// in row sorted binary matrix
class GFG {
static final int N = 4;
// function for finding row with maximum 1
static void findMax(int arr[][]) {
int row = 0, i, j;
for (i = 0, j = N - 1; i < N; i++) {
// find left most position of 1 in
// a row find 1st zero in a row
while (j >= 0 && arr[i][j] == 1) {
row = i;
j--;
}
}
System.out.print("Row number = "
+ (row + 1));
System.out.print(", MaxCount = "
+ (N - 1 - j));
}
// Driver code
public static void main(String[] args) {
int arr[][] = {{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 0},
{0, 1, 1, 1}};
findMax(arr);
}
}
// This code is contributed by Anant Agarwal.
Python3
# python program to find row with
# maximum 1 in row sorted binary
# matrix
N = 4
# function for finding row with
# maximum 1
def findMax (arr):
row = 0
j = N - 1
for i in range(0, N):
# find left most position
# of 1 in a row find 1st
# zero in a row
while (arr[i][j] == 1
and j >= 0):
row = i
j -= 1
print("Row number = " , row + 1,
", MaxCount = ", N - 1 - j)
# driver program
arr = [ [0, 0, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 0],
[0, 1, 1, 1] ]
findMax(arr)
# This code is contributed by Sam007
C#
// C# program to find row with maximum
// 1 in row sorted binary matrix
using System;
class GFG {
static int N = 4;
// function for finding row with maximum 1
static void findMax(int [,]arr)
{
int row = 0, i, j;
for (i = 0, j = N - 1; i < N; i++) {
// find left most position of 1 in
// a row find 1st zero in a row
while (arr[i,j] == 1 && j >= 0)
{
row = i;
j--;
}
}
Console.Write("Row number = " + (row + 1));
Console.Write(", MaxCount = " + (N - 1 - j));
}
// Driver code
public static void Main()
{
int [,]arr = {{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 0},
{0, 1, 1, 1}};
findMax(arr);
}
}
// This code is contributed by nitin mittal
PHP
= 0)
{
$row = $i;
$j--;
}
}
echo "Row number = " , $row + 1;
echo ", MaxCount = " , $N - 1 - $j;
}
// Driver Code
$arr = array(array(0, 0, 0, 1),
array(0, 0, 0, 1),
array(0, 0, 0, 0),
array(0, 1, 1, 1));
findMax($arr);
// This code is contributed by vt_m.
?>
输出:
Row number = 4, MaxCount = 3