-
Notifications
You must be signed in to change notification settings - Fork 1
/
PeakElement.cpp
44 lines (38 loc) · 989 Bytes
/
PeakElement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Leetcode https://leetcode.com/explore/interview/card/top-interview-questions-medium/110/sorting-and-searching/801/
// https://leetcode.com/problems/find-peak-element/solution/
#include <iostream>
#include <vector>
void print(const std::vector<int>& nums)
{
for (auto n : nums) {
std::cout << n << " ";
}
}
int findPeakElement(const std::vector<int>& nums)
{
for (int i = 0; i < nums.size() - 1; ++i) {
if (nums[i] > nums[i + 1]) {
return i;
}
}
return nums.size() - 1;
}
void test(const std::vector<int>& nums)
{
print(nums);
std::cout << "\nPeak element index : " << findPeakElement(nums) << "\n";
}
int main()
{
std::vector<int> nums {1,2,3,1};
test(nums);
std::vector<int> nums2 {1,2,1,3,5,6,4};
test(nums2);
std::vector<int> nums3 {5,4,3,2,1};
test(nums3);
std::vector<int> nums4 {1,2,3,5,6};
test(nums4);
std::vector<int> nums5 {1,1,1,1,1,1};
test(nums5);
return 0;
}