【问题标题】:Find out the row which has the maximum number of 1s in O(n) [duplicate]找出O(n)中1的最大数量的行[重复]
【发布时间】:2011-05-01 13:58:55
【问题描述】:

可能重复:
Matrix with only 1s and 0s

给定一个仅由 1 和 0 组成的 NxN 数组,这样对于每一行,所有 1 都在 0 之前。C

【问题讨论】:

    标签: arrays algorithm


    【解决方案1】:

    前段时间我在这里看到了一个类似的问题,如果有人找到链接,请编辑。 编辑:找到链接:Matrix with only 1s and 0s。解决方案是:

    1. Start in the first row, most-left column
    2. Go right until you hit a 0, if so, go down
    3. If you hit a 1, the current row will be your new "best row"
    4. Repeat from 2 until you either hit the bottom or the right border
    

    给定一个 NxN 数组,这会在最好的情况下检查 N 个单元格,在最坏的情况下检查 N*2-1 个单元格,因此就行/列而言它是 O(N)。

    【讨论】:

      【解决方案2】:

      你的作业解决方案是:

      从第一行的第一个单元格开始。如果它包含 1,则移动到同一行中的下一个单元格。如果它包含 0,则移动到下一行中的相同单元格。重复此操作,直到处理完所有行。

      最后一次在行内移动的行是1个数最多的行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-06
        • 1970-01-01
        • 1970-01-01
        • 2011-08-09
        • 2012-01-02
        • 2011-08-26
        • 2021-11-12
        相关资源
        最近更新 更多