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)

posted @ 2020-02-07 11:18  北叶青藤  阅读(173)  评论(0编辑  收藏  举报