【问题标题】:Which row has the most 1s in a 0-1 matrix with all 1s "on the left"?在 0-1 矩阵中哪一行的 1 最多,所有 1 都在“左边”?
【发布时间】:2011-07-20 07:15:20
【问题描述】:

问题

n x n 矩阵的每一行都由 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) 中做到这一点?

【问题讨论】:

  • 这里的 n 是什么?行数?列数?细胞数?
  • 问题状态为n x n,所以n既是列又是行。

标签: algorithm math matrix


【解决方案1】:

从 1,1 开始。

如果单元格包含 1,那么您是目前为止最长的行;写下来然后向右走。 如果单元格包含 0,则向下。 如果单元格超出范围,则完成。

【讨论】:

    【解决方案2】:

    您可以在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"
    

    【讨论】:

    • 这是错误的......如果你的第一个单元格 [0, 0] 包含一个 0 并且该行中的其余单元格包含所有 1。并且后续行中的所有其余单元格包含只有 0。那么你的答案是 i,其中 i 是最大行数,j 是 0。
    • @Swaranga Sarma:您需要仔细阅读问题所有 1 都在任何 0 之前
    • 代码似乎不正确,特别是"if ( matrix[i][j] == 1 ) j++ max1Row = i" 部分。您没有跟踪最大数量为 1 的行号
    • 如果我们从右到左开始而不是从左到右移动会很好,这样我们可以跳过大部分以 0 结尾的行
    【解决方案3】:

    从第一行开始。保留具有最多 1 的行 RR 的最后 1 的索引 i。在每次迭代中,将当前行与索引 i 上的行 R 进行比较。如果当前行在位置i 上有0,则行R 仍然是答案。 否则,返回当前行的索引。现在我们只需要找到当前行的最后一个 1。从索引i 迭代到当前行的最后一个,将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           "
    

    【讨论】:

      【解决方案4】:

      一些 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++;
      }
      

      【讨论】:

        【解决方案5】:
        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,那么我们将一直到最后一行。

        【讨论】:

        • 你的时间复杂度是O(n^2),因为j=0;// Again start from the first column。 Thom 回答的重点是您不必回到行首,因为您知道如果下一行在当前列中没有1,则它不可能是该行拥有最多1 的。
        猜你喜欢
        • 2011-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多