structhash_table { int key; int val; UT_hash_handle hh; };
typedefstructhash_table* hash_ptr;
structpair { int first; int second; };
structpair* heap; int heapSize;
voidswap(structpair* a, structpair* b) { structpairt = *a; *a = *b, *b = t; }
boolcmp(structpair* a, structpair* b) { return a->second < b->second; }
structpairtop() { return heap[1]; }
intpush(hash_ptr x) { heap[++heapSize].first = x->key; heap[heapSize].second = x->val; int p = heapSize, s; while (p > 1) { s = p >> 1; if (cmp(&heap[s], &heap[p])) return; swap(&heap[p], &heap[s]); p = s; } }
intpop() { heap[1] = heap[heapSize--]; int p = 1, s; while ((p << 1) <= heapSize) { s = p << 1; if (s < heapSize && cmp(&heap[s + 1], &heap[s])) s++; if (cmp(&heap[p], &heap[s])) return; swap(&heap[p], &heap[s]); p = s; } }
int* topKFrequent(int* nums, int numsSize, int k, int* returnSize) { hash_ptr head = NULL; hash_ptr p = NULL, tmp = NULL;
for (int i = 0; i < numsSize; i++) { HASH_FIND_INT(head, &nums[i], p); if (p == NULL) { p = malloc(sizeof(struct hash_table)); p->key = nums[i]; p->val = 1; HASH_ADD_INT(head, key, p); } else { p->val++; } }
functopKFrequent(nums []int, k int) []int { occurrences := map[int]int{} for _, num := range nums { occurrences[num]++ } h := &IHeap{} heap.Init(h) for key, value := range occurrences { heap.Push(h, [2]int{key, value}) if h.Len() > k { heap.Pop(h) } } ret := make([]int, k) for i := 0; i < k; i++ { ret[k - i - 1] = heap.Pop(h).([2]int)[0] } return ret }
classSolution { public: voidqsort(vector<pair<int, int>>& v, int start, int end, vector<int>& ret, int k){ int picked = rand() % (end - start + 1) + start; swap(v[picked], v[start]);
int pivot = v[start].second; int index = start; for (int i = start + 1; i <= end; i++) { if (v[i].second >= pivot) { swap(v[index + 1], v[i]); index++; } } swap(v[start], v[index]);
if (k <= index - start) { qsort(v, start, index - 1, ret, k); } else { for (int i = start; i <= index; i++) { ret.push_back(v[i].first); } if (k > index - start + 1) { qsort(v, index + 1, end, ret, k - (index - start + 1)); } } }
vector<int> topKFrequent(vector<int>& nums, int k){ unordered_map<int, int> occurrences; for (auto& v: nums) { occurrences[v]++; }
structhash_table { int key; int val; UT_hash_handle hh; };
typedefstructhash_table* hash_ptr;
structpair { int first; int second; };
voidswap(structpair* a, structpair* b) { structpairt = *a; *a = *b, *b = t; }
voidsort(structpair* v, int start, int end, int* ret, int* retSize, int k) { int picked = rand() % (end - start + 1) + start; swap(&v[picked], &v[start]);
int pivot = v[start].second; int index = start; for (int i = start + 1; i <= end; i++) { if (v[i].second >= pivot) { swap(&v[index + 1], &v[i]); index++; } } swap(&v[start], &v[index]);
if (k <= index - start) { sort(v, start, index - 1, ret, retSize, k); } else { for (int i = start; i <= index; i++) { ret[(*retSize)++] = v[i].first; } if (k > index - start + 1) { sort(v, index + 1, end, ret, retSize, k - (index - start + 1)); } } }
int* topKFrequent(int* nums, int numsSize, int k, int* returnSize) { hash_ptr head = NULL; hash_ptr p = NULL, tmp = NULL;
for (int i = 0; i < numsSize; i++) { HASH_FIND_INT(head, &nums[i], p); if (p == NULL) { p = malloc(sizeof(struct hash_table)); p->key = nums[i]; p->val = 1; HASH_ADD_INT(head, key, p); } else { p->val++; } } structpairvalues[numsSize]; int valuesSize = 0;