码迷,mamicode.com
首页 > 其他好文 > 详细

find the leftmost column that has number 1

时间:2020-02-07 13:14:09      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:find   when   ase   time   either   hat   bin   first   row   

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

标签:find   when   ase   time   either   hat   bin   first   row   

原文地址:https://www.cnblogs.com/beiyeqingteng/p/12272344.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!