0-1 矩阵中哪一行的 1 最多,且所有 1 都在“左侧”?

发布于 2024-10-25 07:14:22 字数 352 浏览 1 评论 0原文

问题

nxn 矩阵的每一行都由 1 和 0 组成,因此在任何行中,所有 1 都出现在任何 0 之前。在 O(n) 时间内查找包含最多 1 的行。

示例

1 1 1 1 1 0  <- Contains maximum number of 1s, return index 1
1 1 1 0 0 0
1 0 0 0 0 0
1 1 1 1 0 0
1 1 1 1 0 0
1 1 0 0 0 0

我在算法书中发现了这个问题。我能做的最好的事情花费了 O(n logn) 时间。 如何在 O(n) 内做到这一点?

Problem

Each row of an n x n matrix consists of 1's and 0's such that in any row, all 1's come before any 0's. Find row containing most no of 1's in O(n).

Example

1 1 1 1 1 0  <- Contains maximum number of 1s, return index 1
1 1 1 0 0 0
1 0 0 0 0 0
1 1 1 1 0 0
1 1 1 1 0 0
1 1 0 0 0 0

I found this question in my algorithms book. The best I could do took O(n logn) time.
How to do this in O(n)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

‖放下 2024-11-01 07:14:22

从 1,1 开始。

如果单元格包含 1,则您位于迄今为止最长的行;写下来然后向右走。
如果单元格包含 0,则向下。
如果单元格超出范围,则完成。

Start at 1,1.

If the cell contains 1, you're on the longest row so far; write it down and go right.
If the cell contains 0, go down.
If the cell is out of bounds, you're done.

著墨染雨君画夕 2024-11-01 07:14:22

您可以在 O(N) 中完成此操作,如下所示:

A[i][j] 开始,使用 i=j=0

          1, keep moving to the right by doing j++
A[i][j] = 
          0, move down to the next row by doing i++

当您到达最后一行或最后一列时,j 的值将是答案。

伪代码:

Let R be number of rows
Let C be number of columns

Let i = 0
Let j = 0   
Let max1Row = 0

while ( i<R && j<C )
   if ( matrix[i][j] == 1 )
      j++
      max1Row = i
   else
      i++
end-while


print "Max 1's = j"
print "Row number with max 1's = max1Row"

You can do it in O(N) as follows:

Start at A[i][j] with i=j=0.

          1, keep moving to the right by doing j++
A[i][j] = 
          0, move down to the next row by doing i++

When you reach the last row or the last column, the value of j will be the answer.

Pseudo code:

Let R be number of rows
Let C be number of columns

Let i = 0
Let j = 0   
Let max1Row = 0

while ( i<R && j<C )
   if ( matrix[i][j] == 1 )
      j++
      max1Row = i
   else
      i++
end-while


print "Max 1's = j"
print "Row number with max 1's = max1Row"
沉鱼一梦 2024-11-01 07:14:22

从第一行开始。保留 1 数量最多的行 R 以及 R 最后一个 1 的索引 i。在每次迭代中,将当前行与索引 i 上的行 R 进行比较。如果当前行在位置 i 上有 0,则行 R 仍然是答案。
否则,返回当前行的索引。现在我们只需找到当前行的最后 1 个即可。从索引 i 迭代到当前行的最后 1 个,将 R 设置为该行,并将 i 设置为这个新索引。

              i
              |  
              v 
R->   1 1 1 1 1 0  
|
v     1 1 1 0 0 0 (Compare ith index of this row)
      1 0 0 0 0 0         Repeat
      1 1 1 1 0 0           "
      1 1 1 1 0 0           "
      1 1 0 0 0 0           "

Start with the first row. Keep the row R that has the most numbers of 1s and the index i of the last 1 of R. in each iteration compare the current row with the row R on the index i. if the current row has a 0 on position i, the row R is still the answer.
Otherwise, return the index of the current row. Now we just have to find the last 1 of the current row. Iterate from index i up to the last 1 of the current row, set R to this row and i to this new index.

              i
              |  
              v 
R->   1 1 1 1 1 0  
|
v     1 1 1 0 0 0 (Compare ith index of this row)
      1 0 0 0 0 0         Repeat
      1 1 1 1 0 0           "
      1 1 1 1 0 0           "
      1 1 0 0 0 0           "
皓月长歌 2024-11-01 07:14:22

一些 C 代码可以做到这一点。

int n = 6;
int maxones = 0, maxrow = -1, row = 0, col = 0;
while(row < n) {
    while(col < n && matrix[row][col] == 1) col++;
    if(col == n) return row;
    if(col > maxones){
        maxrow = row;
        maxones = col;
    }
    row++;
}

Some C code to do this.

int n = 6;
int maxones = 0, maxrow = -1, row = 0, col = 0;
while(row < n) {
    while(col < n && matrix[row][col] == 1) col++;
    if(col == n) return row;
    if(col > maxones){
        maxrow = row;
        maxones = col;
    }
    row++;
}
天暗了我发光 2024-11-01 07:14:22
int [] getMax1withRow(int [][] matrix){
    int [] result=new int[2];
    int rows=matrix.length;
    int cols=matrix[0].length;
    int i=0, j=0;
    int max_row=0;// This will show row with maximum 1. Intialing pointing to 0th row.
    int max_one=0;// max one
    while(i< rows){
        while(matrix[i][j]==1){
            j++;
        }
        if(j==n){
             result[0]=n;
             result[1]=i;
             return result;
        }
        if(j>max_one){
             max_one=j;
             max_row=i;
        }
        j=0;// Again start from the first column
        i++;// increase row number
    }
    result[0]=max_one;
    result[1]=max_row;
    return result;
}

时间复杂度=> O(row+col),在最坏的情况下,如果除了最后一行有 n 个 1 之外,每行都有 n-1 个,那么我们必须行进到最后一行。

int [] getMax1withRow(int [][] matrix){
    int [] result=new int[2];
    int rows=matrix.length;
    int cols=matrix[0].length;
    int i=0, j=0;
    int max_row=0;// This will show row with maximum 1. Intialing pointing to 0th row.
    int max_one=0;// max one
    while(i< rows){
        while(matrix[i][j]==1){
            j++;
        }
        if(j==n){
             result[0]=n;
             result[1]=i;
             return result;
        }
        if(j>max_one){
             max_one=j;
             max_row=i;
        }
        j=0;// Again start from the first column
        i++;// increase row number
    }
    result[0]=max_one;
    result[1]=max_row;
    return result;
}

Time complexity => O(row+col), In worse case If every row has n-1 one except last row which have n 1s then we have be travel till last row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文