Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

  • Integers in each row are sorted in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

Example:

Consider the following matrix:

[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]

Given target = 5, return true.

Given target = 20, return false.

这个题目思路, 1. brute force  T: O(m*n)    2. T: O(m+n)     3. T: O(lg(n!))  先找第一行, 第一列, 2n 再从(1,1) 开始扫第二行第二列, 2(n-1), .... 所以是lgn + lg (n-1) + lg(n-2)...lg(1) = lg(n!)

1) T: O(m*n)

两个for loop即可.

2) T: O(m + n)

        #O(n + m) , O(1)
if not matrix or len(matrix[0]) == 0: return False
lr, lc = len(matrix), len(matrix[0])
r, c = lr - 1, 0
while r >= 0 and c < lc:
if matrix[r][c] > target:
r -= 1
elif matrix[r][c] < target:
c += 1
else:
return True
return False

3)  T: O(lg(n!))

        # T: O(lg(n!))   S: O(1)
def helper(i, flag):
l = i
r = lrc[0]-1 if flag == 'row' else lrc[1] -1if flag == 'col':
while l + 1 < r:
mid = l + (r - l)//2
if matrix[i][mid] > target:
r = mid
elif matrix[i][mid] < target:
l = mid
else:
return True
if target in [matrix[i][l], matrix[i][r]]:
return True
return False
else:
while l + 1 <r:
mid = l + (r - l)//2
if matrix[mid][i] > target:
r = mid
elif matrix[mid][i] < target:
l = mid
else:
return True
if target in [matrix[l][i], matrix[r][i]]:
return True
return False if not matrix or len(matrix[0]) == 0: return False
lrc = [len(matrix), len(matrix[0])]
for i in range(min(lrc)):
row_check = helper(i, "row")
col_check = helper(i, "col")
if row_check or col_check: return True
return False

[LeetCode] 240. Search a 2D Matrix II_Medium tag: Binary Search的更多相关文章

  1. [LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  2. [LeetCode] 374. Guess Number Higher or Lower_Easy tag: Binary Search

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  3. [LeetCode] 278. First Bad Version_Easy tag: Binary Search

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  4. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  5. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

  6. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

  7. [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  8. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】240. Search a 2D Matrix II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...

随机推荐

  1. A - 小孩报数问题

    有N个小孩围成一圈,给他们从1开始依次编号,现指定从第W个开始报数,报到第S个时,该小孩出列,然后从下一个小孩开始报数,仍是报到S个出列,如此重复下去,直到所有的小孩都出列(总人数不足S个时将循环报数 ...

  2. -----------MSSQL生成流水号-----------------------

    --下面的代码生成长度为8的编号,编号以BH开头,其余6位为流水号.--得到新编号的函数CREATE FUNCTION f_NextBH()RETURNS char(8)ASBEGIN RETURN( ...

  3. Mysql----整理

    --------------------------------------------------数据常库常用操作总结---------------------------------------- ...

  4. Ajax请求参数较长导致请求失败

    Ajax请求参数比较长,第5行参数大概1100个字符吧,是接口的请求报文. $.ajax({ type:"POST", url:"${ctx}/test.action?m ...

  5. ros查看摄像头是否打开正常

    使用rqt_image_view命令,查看摄像头是否正常输出图像

  6. splash 安装

    搞定NVIDIA显卡后,开始弄splash 根据 https://github.com/paperManu/splash 提示 最简安装就是用apt sudo apt install flatpak ...

  7. EF Code First模型约束

    总之,EF比较复杂.如果不想深究,建议简单用用.基本对应就行,大项目标准开发还是ModelFirst(先建立DB各种约束),然后再c#类约束.定义. 当然写原型时用ef很快.

  8. io.UnsupportedOperation: not readable

    两处错误一.你是用open打开一个文件,此时调用的是w写入模式,下面使用read是没有权限的,你得使用w+读写模式二.使用write写入一个字符s,但是此时并没有真正的写入,而是还存在与内存中.此时执 ...

  9. 转:Java 集合详解

    原文地址:https://www.cnblogs.com/ysocean/p/6555373.html 一.集合的由来 通常,我们的程序需要根据程序运行时才知道创建多少个对象.但若非程序运行,程序开发 ...

  10. Linux下MySql的配置文件my.cnf详细 讲解

    经常在使用MySql,但是对于MySql下面的各种参数的配置并不是很熟悉,经常在需要改变某项参数的时候,还要到处在网上查找,有点不方便.今天想把MySql下面的配置文件my.cnf详细的做一个说明(L ...