The trick here is to name the state correctly, then let the implementation follow. For 852. Peak Index in a Mountain Array, the solution in this repository is mainly a two pointers solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: two pointers, sliding window.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are peakIndexInMountainArray.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**
02Let's call an array A a mountain if the following properties hold:
03
04A.length >= 3
05There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
06Given an array that is definitely a mountain, return any i such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1].
07
08Example 1:
09
10Input: [0,1,0]
11Output: 1
12Example 2:
13
14Input: [0,2,1,0]
15Output: 1
16Note:
17
183 <= A.length <= 10000
190 <= A[i] <= 10^6
20A is a mountain, as defined above.
21**/
22
23//Your runtime beats 52.46 % of cpp submissions.
24class Solution {
25public:
26 int peakIndexInMountainArray(vector<int>& A) {
27 //binary search
28 int low = 0;
29 int up = A.size()-1;
30 int cur = A.size()/2;
31 // cout << low << " " << up << " " << cur << endl;
32 while(true){
33 // cout << low << " " << up << " " << cur << endl;
34 if(A[cur-1] < A[cur] && A[cur] < A[cur+1]){ //left side of mountain
35 low = cur;
36 cur = (cur+up)/2;
37 }else if(A[cur-1] > A[cur] && A[cur] > A[cur+1]){ //right side
38 up = cur;
39 cur = (cur+low)/2;
40 }else{
41 return cur;
42 }
43 }
44 }
45};
46
47/**
48Approach 1: Linear Scan
49Intuition and Algorithm
50
51The mountain increases until it doesn't. The point at which it stops increasing is the peak.
52
53//Java
54class Solution {
55 public int peakIndexInMountainArray(int[] A) {
56 int i = 0;
57 while (A[i] < A[i+1]) i++;
58 return i;
59 }
60}
61
62Complexity Analysis
63
64Time Complexity: O(N), where N is the length of A.
65
66Space Complexity: O(1).
67
68Approach 2: Binary Search
69Intuition and Algorithm
70
71The comparison A[i] < A[i+1] in a mountain array looks like [True, True, True, ..., True, False, False, ..., False]:
721 or more boolean Trues, followed by 1 or more boolean False.
73For example, in the mountain array [1, 2, 3, 4, 1], the comparisons A[i] < A[i+1] would be True, True, True, False.
74
75We can binary search over this array of comparisons, to find the largest index i such that A[i] < A[i+1].
76For more on binary search, see the LeetCode explore topic here.
77
78//Your runtime beats 52.46 % of cpp submissions.
79class Solution {
80public:
81 int peakIndexInMountainArray(vector<int>& A) {
82 //binary search
83 int low = 0;
84 int up = A.size()-1;
85 while(low < up){
86 int mi = (low+up)/2;
87 if(A[mi] < A[mi+1]){
88 low = mi+1;
89 }else{
90 up = mi;
91 }
92 }
93 return low;
94 }
95};
96
97Complexity Analysis
98
99Time Complexity: O(logN), where N is the length of A.
100
101Space Complexity: O(1).
102**/
Cost