Advertisement
limimage

852. Peak Index in a Mountain Array

Feb 1st, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int peakIndexInMountainArray(vector<int>& A) {
  4. int l = 1, r = A.size() - 1, mid, ans;
  5. while (l <= r) {
  6. mid = (l + r) >> 1;
  7. if (A[mid] > A[mid - 1]) {
  8. ans = mid, l = mid + 1;
  9. }
  10. else
  11. r = mid - 1;
  12. }
  13. return ans;
  14. }
  15. };
  16.  
  17. //O(log(A.size()) - time complexity, O(A.size()) - memory complexity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement