Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- class Solution {
- public:
- int peakIndexInMountainArray(const std::vector<int>& data) {
- int left_idx = 1;
- int right_idx = data.size() - 2;
- int middle = left_idx + (right_idx - left_idx) / 2;
- while (left_idx < right_idx) {
- if (data[middle] < data[middle + 1]) {
- left_idx = middle + 1;
- } else {
- right_idx = middle;
- }
- middle = left_idx + (right_idx - left_idx) / 2;
- }
- return left_idx;
- }
- };
- // logarithmic time, constant memory
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement