find the leftmost column that has number 1

Posted beiyeqingteng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了find the leftmost column that has number 1相关的知识,希望对你有一定的参考价值。

Given a matrix, like this

[[0, 0, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 0, 1, 1, 1]
[0, 0, 0, 0, 0]]

each cell is either 1 or 0
in each row, from left to right, when you first see a number 1 in the cell, then the rest cells in this row will all be 1
Question: please find the leftmost column that has number 1

解法1:

For each row, do binary search and find the left most column that has number 1.

Time complexity: O(m *  lgn)

解法2:

Start from the top right element, if current value is 1, move left, otherwise, move down.

Time complexity: O(m + n)

以上是关于find the leftmost column that has number 1的主要内容,如果未能解决你的问题,请参考以下文章

ATcoder 2000 Leftmost Ball

Leftmost Digit(hdu1060)(数学题)

Leftmost Ball

HDU 1060 [Leftmost Digit]数学

ACM_Leftmost Digit

AGC002F Leftmost Ball