博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Find Peak Element II
阅读量:5945 次
发布时间:2019-06-19

本文共 2350 字,大约阅读时间需要 7 分钟。

There is an integer matrix which has the following features:

  • The numbers in adjacent positions are different.
  • The matrix has n rows and m columns.
  • For all i < m, A[0][i] < A[1][i] && A[n - 2][i] > A[n - 1][i].
  • For all j < n, A[j][0] < A[j][1] && A[j][m - 2] > A[j][m - 1].

We define a position P is a peek if:

A[j][i] > A[j+1][i] && A[j][i] > A[j-1][i] && A[j][i] > A[j][i+1] && A[j][i] > A[j][i-1]

Given a matrix:

[  [1 ,2 ,3 ,6 ,5],  [16,41,23,22,6],  [15,17,24,21,7],  [14,18,19,20,10],  [13,14,11,10,9]]

return index of 41 (which is [1,1]) or index of 24 (which is [2,2])

思路是先上下二分,后左右二分.复杂度为T(n) = O(n)+O(n/2)+T(n/2) ...T(n) = O(n)+O(n/2)+O(n/2)+O(n/4) ... = 3*(O(n/2)+O(n/4)+...)=O(n)

class Solution:    #@param A: An list of list integer     #@return: The index of position is a list of integer, for example [2,2]    def findPeakII(self, A):        if not A:            return [-1,-1]        l = 1        r = len(A[0]) - 2        up = 1        down = len(A) - 2        import sys        while l + 1 < r and up + 1 < down:            midRow = (up + down)/2            rowMax = - sys.maxint - 1  #max value in the middle row            for i in xrange(l, r + 1): #find the max value in the middle row                if A[midRow][i] > rowMax:                    index1 = i                    rowMax = A[midRow][i]            if A[midRow - 1][index1] < rowMax and A[midRow + 1][index1] < rowMax:                return [midRow, index1]            else:                if A[midRow - 1][index1] > rowMax:                    down = midRow                else:                    up = midRow            colMax = -sys.maxint - 1            for j in xrange(up, down + 1):                if A[j][index1] > colMax:                    index2 = j                    colMax = A[j][index1]            if A[index2][index1 - 1] < colMax and A[index2][index1 + 1] < colMax:                return [index2, index1]            else:                if A[index2][index1 -1 ] > colMax:                    r = index1                else:                    l = index1               if A[up][l] > A[down][l] or A[up][r] > A[down][r]:            first = up        else:            first = down        if A[first][l] > A[first][r]:            second = l        else:            second = r        return [first, second]

 

 

转载于:https://www.cnblogs.com/sherylwang/p/5602711.html

你可能感兴趣的文章
正则表达式匹配数字
查看>>
前端模块化
查看>>
QIBO CMS SQL Injection Via Variable Uninitialization In \member\special.php
查看>>
二维数组---模拟斗地主
查看>>
【转】(DT系列六)devicetree中数据和 struct device有什么关系
查看>>
【前端性能】必须要掌握的原生JS实现JQuery
查看>>
mysql系统变量
查看>>
svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted
查看>>
JavaScript 编码规范(中文/Airbnb公司版)
查看>>
DNX/ASP.NET 5的xUnit入门向导
查看>>
正则表达式—匹配连续重复的字符
查看>>
如何在一个月内改善你的生活
查看>>
beyond compare比较工具设置
查看>>
Java中的事务
查看>>
Spring Ajax一个简单样例
查看>>
传递给数据库 'master' 中的日志扫描操作的日志扫描号无效
查看>>
导入https证书
查看>>
SAP R3和JAVA交换数据之JCO
查看>>
近期给朋友推荐的笔记本型号
查看>>
sqlserver使用存储过程发送http请求
查看>>