<p><em>(This problem is an <strong>interactive problem</strong>.)</em></p>

<p>You may recall that an array <code>arr</code> is a <strong>mountain array</strong> if and only if:</p>

<ul>
	<li><code>arr.length &gt;= 3</code></li>
	<li>There exists some <code>i</code> with <code>0 &lt; i &lt; arr.length - 1</code> such that:
	<ul>
		<li><code>arr[0] &lt; arr[1] &lt; ... &lt; arr[i - 1] &lt; arr[i]</code></li>
		<li><code>arr[i] &gt; arr[i + 1] &gt; ... &gt; arr[arr.length - 1]</code></li>
	</ul>
	</li>
</ul>

<p>Given a mountain array <code>mountainArr</code>, return the <strong>minimum</strong> <code>index</code> such that <code>mountainArr.get(index) == target</code>. If such an <code>index</code> does not exist, return <code>-1</code>.</p>

<p><strong>You cannot access the mountain array directly.</strong> You may only access the array using a <code>MountainArray</code> interface:</p>

<ul>
	<li><code>MountainArray.get(k)</code> returns the element of the array at index <code>k</code> (0-indexed).</li>
	<li><code>MountainArray.length()</code> returns the length of the array.</li>
</ul>

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

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

<p><strong class="example">Example 2:</strong></p>

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

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= mountain_arr.length() &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= target &lt;= 10<sup>9</sup></code></li>
	<li><code>0 &lt;= mountain_arr.get(index) &lt;= 10<sup>9</sup></code></li>
</ul>

# """
# This is MountainArray's API interface.
# You should not implement it, or speculate about its implementation
# """
#class MountainArray:
#    def get(self, index: int) -> int:
#    def length(self) -> int:

class Solution:
    def findInMountainArray(self, target: int, mountain_arr: 'MountainArray') -> int:
        """
        1) find mountain peak
        2) find target from 0 -> peak
        3) find target from peak -> end
        """
        end_idx =  mountain_arr.length() -1 
        l, r = 0, end_idx
        peak_idx = None
        while l <= r:
            m = l + ( r - l ) // 2
            m_val = mountain_arr.get(m)

            if m == 0:
                l = m + 1
                continue

            if m == end_idx:
                r = m -1
                continue

            l_val = mountain_arr.get(m-1)
            r_val = mountain_arr.get(m + 1)

            if l_val < m_val < r_val:
                l = m + 1
                continue

            if l_val > m_val > r_val:
                r = m -1
                continue
            
            peak_idx = m
            break

        print(f" peak idx : {peak_idx} ")
        
        l, r = 0, peak_idx
        while l <= r:
            m = l + ( r - l ) // 2
            m_val = mountain_arr.get(m)
            if m_val == target:
                return m

            if m_val > target:
                r = m -1
            else:
                l = m + 1

        l, r = peak_idx, end_idx
        while l <= r:
            m = l + ( r - l ) // 2
            m_val = mountain_arr.get(m)
            if m_val == target:
                return m

            if m_val > target:
                l = m + 1
            else:
                r = m - 1
        return -1


            

17400