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. CodeForces - 748E (枚举+脑洞)

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  2. 转载:浅析@PathVariable 和 @RequestParam

    在网上看了一篇很好的文章,讲的很清楚明了,说到了点子上(转自:https://blog.csdn.net/chuck_kui/article/details/55506723): 首先 上两个地址: ...

  3. okvis代码解读

    okvis_timing模块 提供时间转换的功能函数 okvis_util模块 包含 assert_macros.hpp  该文件包含一些有用的断言宏. source_file_pos.hpp 该文件 ...

  4. 动态环境下的slam问题如何解决?

    作者:颜沁睿链接:https://www.zhihu.com/question/47817909/answer/107775045来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  5. [No000011E]Python教程1/9-Python简介

    Python是一种计算机程序设计语言.你可能已经听说过很多种流行的编程语言,比如非常难学的C语言,非常流行的Java语言,适合初学者的Basic语言,适合网页编程的JavaScript语言等等. 那P ...

  6. [No0000112]ComputerInfo,C#获取计算机信息(cpu使用率,内存占用率,硬盘,网络信息)

    github地址:https://github.com/charygao/SmsComputerMonitor 软件用于实时监控当前系统资源等情况,并调用接口,当资源被超额占用时,发送警报到个人手机: ...

  7. 查看CUDA和cuDNN的版本号

    1.查看cuda版本 cat /usr/local/cuda/version.txt2.查看cudnn版本 cat /usr/local/cuda/include/cudnn.h | grep CUD ...

  8. cocoapods 无法 升级 repo 无法执行pod install命令

    首先MAC自带了ruby,他是用rvm 管理的, 如果你用homebrew又安装了一个新版,很可能在升级cocoapods时遇到问题,明明自己用homebrew升级到最新版本了,页配置环境变量了,为什 ...

  9. MySQL5.7免安装版配置详细教程

    MySQL5.7免安装版配置详细教程 一. 软件下载 Mysql是一个比较流行且很好用的一款数据库软件,如下记录了我学习总结的mysql免安装版的配置经验,要安装的朋友可以当做参考哦 mysql5.7 ...

  10. H2O 网址

    使用pysparking的一个例子 http://docs.h2o.ai/h2o-tutorials/latest-stable/tutorials/sparkling-water/index.htm ...