-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolution.cpp
51 lines (46 loc) · 1.01 KB
/
Solution.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
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <tuple>
#include <deque>
#include <unordered_map>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
class Solution_my {
public:
int findLucky(vector<int>& arr) {
int res = -1, f = 0;
for (int i = 0; i < arr.size(); i ++ ) {
f = 0;
int target = arr[i];
for (int j : arr) {
if (j == target) f ++ ;
}
if ( f == target ) {
res = target > res ? target : res;
}
}
return res;
}
};
class Solution {
public:
int findLucky(vector<int>& arr) {
map<int, int> cnt;
for (int i : arr) cnt[i] ++ ;
vector<int> res;
for ( auto & [k, v] : cnt ) {
if (k == v) res.push_back(k);
}
if (res.empty()) return -1;
return *max_element(res.begin(), res.end());
}
};
int main() {
Solution a;
return 0;
}