-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-703.cpp
More file actions
33 lines (30 loc) · 846 Bytes
/
LeetCode-703.cpp
File metadata and controls
33 lines (30 loc) · 846 Bytes
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
/*************************************************************************
> File Name: LeetCode-703.cpp
> Author: ltw
> Mail: 3245849061@qq.com
> Created Time: Sat 16 May 2020 06:28:12 PM CST
************************************************************************/
#include<iostream>
using namespace std;
class KthLargest {
public:
typedef pair<int, int> PII;
set<PII> s;
int k, t;
KthLargest(int k, vector<int>& nums) {
this->k = k;
for (int i = 0; i < nums.size(); i++) {
add(nums[i]);
}
return ;
}
int add(int val) {
if (s.size() < k) {
s.insert(PII(val, t++));
} else if (val > s.begin()->first) {
s.erase(s.begin());
s.insert(PII(val, t++));
}
return s.begin()->first;
}
};