Sunday, September 13, 2020

1095. Find in Mountain Array

 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

    /**
     * // This is the MountainArray's API interface.
     * // You should not implement it, or speculate about its implementation
     * class MountainArray {
     *   public:
     *     int get(int index);
     *     int length();
     * };
     */

    class Solution {
    public:
        int findInMountainArray(int target, MountainArray &mountainArr) {
            int peak_idx = findpeak(mountainArr);
            if (peak_idx == -1) {
                return -1;
            }
            
            int left_idx = binary_search(mountainArr, 0, peak_idx, target);
            if (left_idx != -1) {
                return left_idx;
            }
            return binary_search_opposite(mountainArr, peak_idx + 1, mountainArr.length() - 1, target);
            
            
        }
        
        int findpeak(MountainArray &mountainArr) {
            int st = 0;
            int end = mountainArr.length() - 1;
            
            while (st < end) {
                int mid = st + (end - st)/2;
                if (mid == 0) {
                    return -1;
                }
                int mid_elem = mountainArr.get(mid);
                if (mid_elem > mountainArr.get(mid + 1) &&
                    mid_elem > mountainArr.get(mid - 1)) {
                    return mid;
                } else if (mid_elem > mountainArr.get(mid - 1)) {
                    st = mid + 1;
                } else {
                    end = mid;
                }
            }
            return -1;
        }
        
        int binary_search(MountainArray &mountainArr, int start, int end_idx, int target) {
            int st = start;
            int end = end_idx;
            
            while (st <= end) {
                int mid = st + (end - st)/2;
                int mid_elem = mountainArr.get(mid);
                if (mid_elem == target) {
                    return mid;
                } else if (mid_elem < target) {
                    st = mid + 1;
                } else {
                    end = mid - 1;
                }
            }
            return -1;
        }
        
        int binary_search_opposite(MountainArray &mountainArr, int start, int end_idx, int target) {
            int st = start;
            int end = end_idx;
            
            while (st <= end) {
                int mid = st + (end - st)/2;
                int mid_elem = mountainArr.get(mid);
                if (mid_elem == target) {
                    return mid;
                } else if (mid_elem > target) {
                    st = mid + 1;
                } else {
                    end = mid - 1;
                }
            }
            return -1;
        }
        
    };

    No comments:

    Post a Comment