Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
gitgou
LeetCode
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
LeetCode
/
KthLargest.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
65 lines (56 loc) · 1.56 KB
master
Breadcrumbs
LeetCode
/
KthLargest.cpp
Copy path
Top
File metadata and controls
Code
Blame
65 lines (56 loc) · 1.56 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include<vector>
#include<queue>
#include<functional>
using namespace std;
//如果使用排序,每次插入的复杂度最小为o(n), 插入查找时查找可以实现o(log(n)),但是移动元素依然需要o(n)。
//而使用大小顶堆进行插入,调整堆的时间复杂度为o(log(n)),所以最好选用大小顶堆来做选择最大或者最小值。
//因为返回之后不会删除所有返回的数,使用add操作时会导堆膨胀,所以应该维护一个大小为k的。
class KthLargest {
public:
KthLargest(int k, vector<int>& nums)
:_k(k)
{
//建堆
for(auto & i : nums)
{
add(i);
}
}
int add(int val)
{
_data.push(val);
if(_data.size() > _k)
{
_data.pop();
}
return _data.top();
}
/* 使用vector排序, 时间复杂度高
int add(int val)
{
_data.push_back(val);
sort(_data.begin(), _data.end(), std::greater<int>());
if(_data.size() > _k)
{
_data.pop_back();
}
return _data.back();
}
*/
private:
int _k;
//优先级队列,建立一个小顶堆, 底层使用堆排序实现
priority_queue<int, vector<int>, std::greater<int> > _data;
};
int main()
{
vector<int> vec = {4,5,8,2};
KthLargest* k = new KthLargest(3, vec);
cout << k->add(3) << endl;
cout << k->add(5) << endl;
cout << k->add(10) << endl;
cout << k->add(9) << endl;
cout << k->add(4) << endl;
return 0;
}
You can’t perform that action at this time.