-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_the_peaks.cpp
49 lines (45 loc) · 1.27 KB
/
find_the_peaks.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
45
46
47
48
49
/*
* =====================================================================================
*
* Filename: find_the_peaks.cpp
*
* Description: 2951. Find the Peaks. https://leetcode.com/problems/find-the-peaks/
*
* Version: 1.0
* Created: 12/09/2023 20:02:42
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), [email protected]
* Organization:
*
* =====================================================================================
*/
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::pair;
using std::vector;
class Solution {
public:
vector<int> findPeaks(vector<int>& mountain) {
vector<int> peaks;
peaks.reserve(mountain.size());
for (int i = 1; i < mountain.size() - 1; i++) {
if (mountain[i] > mountain[i - 1] && mountain[i] > mountain[i + 1]) {
peaks.push_back(i);
}
}
return peaks;
}
};
TEST(Solution, findPeaks) {
vector<pair<vector<int>, vector<int>>> cases = {
std::make_pair(vector<int>{2, 4, 4}, vector<int>{}),
std::make_pair(vector<int>{1, 4, 3, 8, 5}, vector<int>{1, 3}),
};
for (auto& c : cases) {
EXPECT_THAT(Solution().findPeaks(c.first), testing::ElementsAreArray(c.second));
}
}