-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add top_k.c and refactor top_k.js (#889)
* Add top_k.c based on my_heap.c * Improve the implementation of top_k.js * Add a comment to top_k
- Loading branch information
Showing
13 changed files
with
195 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
add_executable(my_heap my_heap.c) | ||
|
||
add_executable(my_heap_test my_heap_test.c) | ||
add_executable(top_k top_k.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* File: my_heap_test.c | ||
* Created Time: 2023-01-15 | ||
* Author: Reanon ([email protected]) | ||
*/ | ||
|
||
#include "my_heap.c" | ||
|
||
/* Driver Code */ | ||
int main() { | ||
/* 初始化堆 */ | ||
// 初始化大顶堆 | ||
int nums[] = {9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2}; | ||
MaxHeap *heap = newMaxHeap(nums, sizeof(nums) / sizeof(int)); | ||
printf("输入数组并建堆后\n"); | ||
printHeap(heap->data, heap->size); | ||
|
||
/* 获取堆顶元素 */ | ||
printf("\n堆顶元素为 %d\n", peek(heap)); | ||
|
||
/* 元素入堆 */ | ||
push(heap, 7); | ||
printf("\n元素 7 入堆后\n"); | ||
printHeap(heap->data, heap->size); | ||
|
||
/* 堆顶元素出堆 */ | ||
int top = pop(heap); | ||
printf("\n堆顶元素 %d 出堆后\n", top); | ||
printHeap(heap->data, heap->size); | ||
|
||
/* 获取堆大小 */ | ||
printf("\n堆元素数量为 %d\n", size(heap)); | ||
|
||
/* 判断堆是否为空 */ | ||
printf("\n堆是否为空 %d\n", isEmpty(heap)); | ||
|
||
// 释放内存 | ||
freeMaxHeap(heap); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* File: top_k.c | ||
* Created Time: 2023-10-26 | ||
* Author: Krahets (krahets163.com) | ||
*/ | ||
|
||
#include "my_heap.c" | ||
|
||
/* 元素入堆 */ | ||
void pushMinHeap(MaxHeap *maxHeap, int val) { | ||
// 元素取反 | ||
push(maxHeap, -val); | ||
} | ||
|
||
/* 元素出堆 */ | ||
int popMinHeap(MaxHeap *maxHeap) { | ||
// 元素取反 | ||
return -pop(maxHeap); | ||
} | ||
|
||
/* 访问堆顶元素 */ | ||
int peekMinHeap(MaxHeap *maxHeap) { | ||
// 元素取反 | ||
return -peek(maxHeap); | ||
} | ||
|
||
/* 取出堆中元素 */ | ||
int *getMinHeap(MaxHeap *maxHeap) { | ||
// 将堆中所有元素取反并存入 res 数组 | ||
int *res = (int *)malloc(maxHeap->size * sizeof(int)); | ||
for (int i = 0; i < maxHeap->size; i++) { | ||
res[i] = -maxHeap->data[i]; | ||
} | ||
return res; | ||
} | ||
|
||
// 基于堆查找数组中最大的 k 个元素的函数 | ||
int *topKHeap(int *nums, int sizeNums, int k) { | ||
// 初始化小顶堆 | ||
// 请注意:我们将堆中所有元素取反,从而用大顶堆来模拟小顶堆 | ||
int empty[0]; | ||
MaxHeap *maxHeap = newMaxHeap(empty, 0); | ||
// 将数组的前 k 个元素入堆 | ||
for (int i = 0; i < k; i++) { | ||
pushMinHeap(maxHeap, nums[i]); | ||
} | ||
// 从第 k+1 个元素开始,保持堆的长度为 k | ||
for (int i = k; i < sizeNums; i++) { | ||
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆 | ||
if (nums[i] > peekMinHeap(maxHeap)) { | ||
popMinHeap(maxHeap); | ||
pushMinHeap(maxHeap, nums[i]); | ||
} | ||
} | ||
int *res = getMinHeap(maxHeap); | ||
// 释放内存 | ||
freeMaxHeap(maxHeap); | ||
return res; | ||
} | ||
|
||
/* Driver Code */ | ||
int main() { | ||
int nums[] = {1, 7, 6, 3, 2}; | ||
int k = 3; | ||
int sizeNums = sizeof(nums) / sizeof(nums[0]); | ||
|
||
int *res = topKHeap(nums, sizeNums, k); | ||
printf("最大的 %d 个元素为: ", k); | ||
printArray(res, k); | ||
|
||
free(res); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters