Finance[US] Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
460 views
in Coding Questions by Goeduhub's Expert (2.3k points)

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:

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

    1 Answer

    0 like 0 dislike
    by Goeduhub's Expert (2.3k points)
     
    Best answer

    Each binary search requies about log(10000) / log(2) = 13 times of query.

    With 100 as the upper limit, we can do at least 100 / 13 = 7 rounds of binary searches. That is way more than what we need.

    First use binary search to find the peak.

    Then use bianry search to find the target left to the peak; if not found, search the target right to the peak.

    import functools

    class Solution:

        def findInMountainArray(self, target: int, mountain_arr: 'MountainArray') -> int:

            LEN = mountain_arr.length()

            @functools.lru_cache(LEN)

            def get(i):

                return mountain_arr.get(i)

            

            def getPeakIndex(start, end):

                while start < end:

                    mid = (end - start) // 2 + start

                    v = get(mid)

                    if get(mid - 1) < v > get(mid + 1):

                        return mid

                    if get(mid - 1) < v < get(mid + 1):

                        start = mid + 1

                    else:

                        end = mid

            

            def binarySearch(start, end, is_inc):

                while start < end:

                    mid = (end - start) // 2 + start

                    v = get(mid)

                    if v == target:

                        return mid

                    if (v < target) == is_inc:

                        start = mid + 1

                    else:

                        end = mid

                return -1

            peak_ind = getPeakIndex(0, LEN)

            if get(peak_ind) == target:

                return peak_ind

            res = binarySearch(0, peak_ind, is_inc=True)

            if res != -1:

                return res

            return binarySearch(peak_ind + 1, LEN, is_inc=False)

    eg : array = [1,2,3,4,5,3,1]

    target = 3

    ans = Solution()

    ans.findInMountainArray(target, array)

    Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

    Best Data Science Online Courses[Lists] on:-

    Claim your 10 Days FREE Trial for Pluralsight.

    Best Data Science Courses on Datacamp
    Best Data Science Courses on Coursera
    Best Data Science Courses on Udemy
    Best Data Science Courses on Pluralsight
    Best Data Science Courses & Microdegrees on Udacity
    Best Artificial Intelligence[AI] Courses on Coursera
    Best Machine Learning[ML] Courses on Coursera
    Best Python Programming Courses on Coursera
    Best Artificial Intelligence[AI] Courses on Udemy
    Best Python Programming Courses on Udemy

    Related questions

    0 like 0 dislike
    1 answer 1.7k views
    0 like 0 dislike
    1 answer 317 views
    0 like 0 dislike
    1 answer 820 views
    0 like 0 dislike
    1 answer 981 views
    0 like 0 dislike
    1 answer 1.7k views
    asked Sep 10, 2020 in Coding Questions by Vaibhav98 Goeduhub's Expert (2.3k points)

     Important Lists:

    Important Lists, Exams & Cutoffs Exams after Graduation PSUs

     Goeduhub:

    About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

     

    Free Online Directory

    ...