-
Notifications
You must be signed in to change notification settings - Fork 0
/
third_maximum_number.cpp
51 lines (46 loc) · 1.27 KB
/
third_maximum_number.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
50
51
/*
* =====================================================================================
*
* Filename: third_maximum_number.cpp
*
* Description: 414. Third Maximum Number
* https://leetcode.com/problems/third-maximum-number/
*
* Version: 1.0
* Created: 06/15/2024 09:43:18
* Revision: none
* Compiler: gcc
*
* Author: [email protected]
* Organization:
*
* =====================================================================================
*/
#include <set>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::vector;
class Solution {
public:
int thirdMax(vector<int>& nums) {
std::set<int> s;
for (const int n : nums) {
s.insert(n);
}
if (s.size() > 2) {
s.erase(*s.rbegin());
s.erase(*s.rbegin());
}
return *s.rbegin();
}
};
TEST(Solution, thirdMax) {
vector<std::pair<vector<int>, int>> cases = {std::make_pair(vector<int>{3, 2, 1}, 1),
std::make_pair(vector<int>{1, 2}, 2),
std::make_pair(vector<int>{2, 2, 3, 1}, 1)};
for (auto& c : cases) {
EXPECT_EQ(Solution().thirdMax(c.first), c.second);
}
}