zoukankan      html  css  js  c++  java
  • 【leetcode】1095. Find in Mountain Array

    题目如下:

    (This problem is an interactive problem.)

    You may recall that an array A is a mountain array if and only if:

    • A.length >= 3
    • There exists some i with 0 < i < A.length - 1 such that:
      • A[0] < A[1] < ... A[i-1] < A[i]
      • A[i] > A[i+1] > ... > A[A.length - 1]

    Given a mountain array mountainArr, return the minimum index such that mountainArr.get(index) == target.  If such an index doesn't exist, return -1.

    You can't access the mountain array directly.  You may only access the array using a MountainArray interface:

    • MountainArray.get(k) returns the element of the array at index k (0-indexed).
    • MountainArray.length() returns the length of the array.

    Submissions making more than 100 calls to MountainArray.get will be judged Wrong Answer.  Also, any solutions that attempt to circumvent the judge will result in disqualification.

    Example 1:

    Input: array = [1,2,3,4,5,3,1], target = 3
    Output: 2
    Explanation: 3 exists in the array, at index=2 and index=5. Return the minimum index, which is 2.

    Example 2:

    Input: array = [0,1,2,4,2,1], target = 3
    Output: -1
    Explanation: 3 does not exist in the array, so we return -1.
    

    Constraints:

    1. 3 <= mountain_arr.length() <= 10000
    2. 0 <= target <= 10^9
    3. 0 <= mountain_arr.get(index) <= 10^9
     

    解题思路:我的解法是二分查找。mountain array 数组的特点是有一个顶点,顶点左边的区间是单调递增,右边的区间是单调递减。所以首先是找出顶点的下标,对于任意一个点mid,如果值比(mid-1)和(mid+1)都大,表示这个是顶点;如果mid的值大于(mid+1),表示mid处于下降区间,令high = mid - 1;如果mid的值大于(mid-1),表示mid处于上升区间,令low = mid + 1;最终可以计算出顶点top。接下来再对左边的上升区间做二分查找求target,如果找到则返回对应小标;没有的话继续对右边的下降区间用二分查找。

    代码如下:

    # """
    # This is MountainArray's API interface.
    # You should not implement it, or speculate about its implementation
    # """
    #class MountainArray(object):
    #    def get(self, index):
    #        """
    #        :type index: int
    #        :rtype int
    #        """
    #
    #    def length(self):
    #        """
    #        :rtype int
    #        """
    
    class Solution(object):
        def findInMountainArray(self, target, mountain_arr):
            """
            :type target: integer
            :type mountain_arr: MountainArray
            :rtype: integer
            """
            length = mountain_arr.length()
            low = 0
            high = length - 1
            while low <= high:
                mid = (low + high)/2
                mid_val = mountain_arr.get(mid)
                mid_l_val,mid_h_val = -float('inf'),-float('inf')
                if mid - 1 >= 0:
                    mid_l_val = mountain_arr.get(mid-1)
                if mid + 1 <= high:
                    mid_h_val = mountain_arr.get(mid+1)
                if mid_val > mid_l_val and mid_val > mid_h_val:
                    break
                elif mid_val > mid_h_val:
                    high = mid - 1
                elif mid_val > mid_l_val:
                    low = mid + 1
            #left
            low,high = 0,mid
            res = -1
            while low <= high:
                mid = (low + high)/2
                mid_val = mountain_arr.get(mid)
                if target == mid_val:
                    res = mid
                    break
                elif target > mid_val:
                    low = mid + 1
                else:
                    high = mid - 1
    
            if res != -1:return res
            low, high = mid,length-1
            while low <= high:
                mid = (low + high)/2
                mid_val = mountain_arr.get(mid)
                if target == mid_val:
                    res = mid
                    break
                elif target < mid_val:
                    low = mid + 1
                else:
                    high = mid - 1
            return res
  • 相关阅读:
    认识hasLayout——IE浏览器css bug的一大罪恶根源 转
    web网站p教程 转
    可遇不可求的Question之SQLServer的INSERT ON DUPLICATE KEY UPDATE语法篇
    可遇不可求的Question之Protoc.exe无法编译proto文件篇
    python各个版本的下载地址
    可遇不可求的Question之WCF发布无法运行篇
    [转]理解JSON:3分钟课程
    可遇不可求的Question之Silverlight发布IIS设置篇
    可遇不可求的Question之C#中的匿名事件导致内存泄露的解决篇
    可遇不可求的Question之mysql连接数暴增的解决方法篇
  • 原文地址:https://www.cnblogs.com/seyjs/p/11075534.html
Copyright © 2011-2022 走看看