Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include <vector>
  2.  
  3. class Solution {
  4. public:
  5.     int peakIndexInMountainArray(const std::vector<int>& data) {
  6.         int left_idx = 1;
  7.         int right_idx = data.size() - 2;
  8.         int middle = left_idx + (right_idx - left_idx) / 2;
  9.         while (left_idx < right_idx) {
  10.             if (data[middle] < data[middle + 1]) {
  11.                 left_idx = middle + 1;
  12.             } else {
  13.                 right_idx = middle;
  14.             }
  15.             middle = left_idx + (right_idx - left_idx) / 2;
  16.         }
  17.         return left_idx;
  18.     }
  19. };
  20.  
  21.  
  22. // logarithmic time, constant memory
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement