- 215. 数组中的第K个最大元素(快排+二分)">215. 数组中的第K个最大元素(快排+二分)
- 347. 前 K 个高频元素(堆排,桶排)">347. 前 K 个高频元素(堆排,桶排)
- 451. 根据字符出现频率排序(hash)">451. 根据字符出现频率排序(hash)
215. 数组中的第K个最大元素(快排+二分)
使用的快速排序加二分
这里出现了很多错误,建议还是使用快排的标准模式。
注意使用数组随机化,防止最坏情况的出现,时间代价的期望是O(n)
快排写法
class Solution {
public:
void randomsort(vector<int>& nums){//数组随机化
for(int i = 0; i < nums.size(); i++){
int j = rand()%nums.size();
swap(nums[i], nums[j]);
}
}
int findKthLargest(vector<int>& nums, int k) {
srand(time(0));
randomsort(nums);
int l = 0, n = nums.size(), r = nums.size();
while(l < r){
int mid = quick_sort(nums, l , r);
if(mid == n - k){
return nums[mid];
} else if(mid > n-k){
r = mid;
} else {
l = mid + 1;
}
}
return nums[l];
}
int quick_sort(vector<int>& nums, int l, int r){
if(l+1 >= r){
return l;
}
int p = l, q = r - 1, key = nums[l];
while(p < q){
while(p < q&&nums[q] >= key){
q--;
}
nums[p] = nums[q];
while(p < q&&nums[p] <= key){
p++;
}
nums[q] = nums[p];
}
nums[p] = key;
return p;
}
};
堆排写法
堆排写法效率高一点。
class Solution {
public:
void maxHeapify(vector<int>& a, int i, int heapSize) {
int l = i * 2 + 1, r = i * 2 + 2, largest = i;
if (l < heapSize && a[l] > a[largest]) {
largest = l;
}
if (r < heapSize && a[r] > a[largest]) {
largest = r;
}
if (largest != i) {
swap(a[i], a[largest]);
maxHeapify(a, largest, heapSize);
}
}
void buildMaxHeap(vector<int>& a, int heapSize) {
for (int i = heapSize / 2; i >= 0; --i) {
maxHeapify(a, i, heapSize);
}
}
int findKthLargest(vector<int>& nums, int k) {
int heapSize = nums.size();
buildMaxHeap(nums, heapSize);
for (int i = nums.size() - 1; i >= nums.size() - k + 1; --i) {
swap(nums[0], nums[i]);
--heapSize;
maxHeapify(nums, 0, heapSize);//这里每次将堆顶值与最小值进行调换,但是只调整堆顶值,来模拟删除的效果。
}
return nums[0];
}
};
347. 前 K 个高频元素(堆排,桶排)
堆排
使用优先队列实现堆排。
时间复杂度:O(klogn)
class Solution {
public:
static bool cmp(pair<int, int>& m, pair<int, int>& n) {
return m.second < n.second;
}
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> occurrences;
for (auto& v : nums) {
occurrences[v]++;
}
// pair 的第一个元素代表数组的值,第二个元素代表了该值出现的次数
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(&cmp)> q(cmp);
for (auto& [num, count] : occurrences) {
q.emplace(num, count);
}
vector<int> ret;
while (!q.empty()) {
if(ret.size() == k){
break;
}
ret.emplace_back(q.top().first);
q.pop();
}
return ret;
}
};
桶排
用桶装相同的数,频次越大桶越大。维护一个max_count是桶的最大值,维护一个buckets,第一维桶的大小,第二维是有这个频次的元素。max_count慢慢往下减直到获得k个元素,满足条件。
时间复杂度:O(N)
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> counts;
int max_count = 0;
for(const int & num : nums){
max_count = max(max_count, ++counts[num]);
}
vector<vector<int>> buckets(max_count+1);
for(const auto &p : counts){
buckets[p.second].push_back(p.first);
}
vector<int> ans;
for(int i = max_count; i >= 0&&ans.size() < k; --i){
for(const int &num : buckets[i]){
ans.push_back(num);
if(ans.size() == k) {
break;
}
}
}
return ans;
}
};
451. 根据字符出现频率排序(hash)
哈希sort
缺点:花费时间较多,因为哈希表的取数据虽然时间复杂度为O(1),但是嵌入sort函数中作为cmp函数的条件的话会被调用nlogn次,花费很多时间。
优点:代码简洁
class Solution {
public:
string frequencySort(string s) {
unordered_map<char, int> mp;
for(auto ch:s) mp[ch]++;
sort(s.begin(),s.end(),[&](const char &a, const char &b){
return mp[a]==mp[b] ? a>b:mp[a] > mp[b];
});
return s;
}
};
//此处耗时久的原因是因为,hash表的查询也需要时间。
使用迭代器
class Solution {
public:
string frequencySort(string s) {
string ans;
unordered_map<char, int> hash;
for(auto c : s){
hash[c]++;
}
vector<pair<char, int>> vec;
for(auto it = hash.begin(); it != hash.end(); it++){
vec.push_back(*it);
}
sort(vec.begin(), vec.end(),cmp);
for(auto i = vec.begin(); i != vec.end(); i++){
for(int j = 0; j < i->second; j++){
ans+=i->first;
}
}
return ans;
}
static bool cmp(pair<char, int> & a, pair<char, int> &b){
return a.second > b.second;
}
};