Add kotlin code for the chapter of heap (#1115)
* feat(kotlin): add kotlin code for dynamic programming. * Update knapsack.kt * feat(kotlin): add kotlin codes for graph. * style(kotlin): reformatted the codes. * feat(kotlin): add kotlin codes for the chapter of greedy. * Update max_product_cutting.kt * feat(kotlin): add kotlin code for chapter of hashing. * style(kotlin): modified some comment * Update array_hash_map.kt * Update hash_map_chaining.kt * Update hash_map_chaining.kt * feat(kotlin): add kotlin codes for the chapter of heap. * Update my_heap.ktpull/1140/head
parent
9769e14017
commit
a05192ea0f
@ -0,0 +1,66 @@
|
||||
/**
|
||||
* File: heap.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_heap
|
||||
|
||||
import utils.printHeap
|
||||
import java.util.*
|
||||
|
||||
fun testPush(heap: Queue<Int>, value: Int) {
|
||||
heap.offer(value) // 元素入堆
|
||||
print("\n元素 $value 入堆后\n")
|
||||
printHeap(heap)
|
||||
}
|
||||
|
||||
fun testPop(heap: Queue<Int>) {
|
||||
val value = heap.poll() // 堆顶元素出堆
|
||||
print("\n堆顶元素 $value 出堆后\n")
|
||||
printHeap(heap)
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* 初始化堆 */
|
||||
// 初始化小顶堆
|
||||
val minHeap: PriorityQueue<Int>
|
||||
|
||||
// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
|
||||
val maxHeap = PriorityQueue { a: Int, b: Int -> b - a }
|
||||
|
||||
println("\n以下测试样例为大顶堆")
|
||||
|
||||
/* 元素入堆 */
|
||||
testPush(maxHeap, 1)
|
||||
testPush(maxHeap, 3)
|
||||
testPush(maxHeap, 2)
|
||||
testPush(maxHeap, 5)
|
||||
testPush(maxHeap, 4)
|
||||
|
||||
/* 获取堆顶元素 */
|
||||
val peek = maxHeap.peek()
|
||||
print("\n堆顶元素为 $peek\n")
|
||||
|
||||
/* 堆顶元素出堆 */
|
||||
testPop(maxHeap)
|
||||
testPop(maxHeap)
|
||||
testPop(maxHeap)
|
||||
testPop(maxHeap)
|
||||
testPop(maxHeap)
|
||||
|
||||
/* 获取堆大小 */
|
||||
val size = maxHeap.size
|
||||
print("\n堆元素数量为 $size\n")
|
||||
|
||||
/* 判断堆是否为空 */
|
||||
val isEmpty = maxHeap.isEmpty()
|
||||
print("\n堆是否为空 $isEmpty\n")
|
||||
|
||||
/* 输入列表并建堆 */
|
||||
// 时间复杂度为 O(n) ,而非 O(nlogn)
|
||||
minHeap = PriorityQueue(mutableListOf<Int?>(1, 3, 2, 5, 4))
|
||||
println("\n输入列表并建立小顶堆后")
|
||||
printHeap(minHeap)
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* File: top_k.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_heap
|
||||
|
||||
import utils.printHeap
|
||||
import java.util.*
|
||||
|
||||
/* 基于堆查找数组中最大的 k 个元素 */
|
||||
fun topKHeap(nums: IntArray, k: Int): Queue<Int> {
|
||||
// 初始化小顶堆
|
||||
val heap = PriorityQueue<Int>()
|
||||
// 将数组的前 k 个元素入堆
|
||||
for (i in 0..<k) {
|
||||
heap.offer(nums[i])
|
||||
}
|
||||
// 从第 k+1 个元素开始,保持堆的长度为 k
|
||||
for (i in k..<nums.size) {
|
||||
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||
if (nums[i] > heap.peek()) {
|
||||
heap.poll()
|
||||
heap.offer(nums[i])
|
||||
}
|
||||
}
|
||||
return heap
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
val nums = intArrayOf(1, 7, 6, 3, 2)
|
||||
val k = 3
|
||||
val res = topKHeap(nums, k)
|
||||
println("最大的 $k 个元素为")
|
||||
printHeap(res)
|
||||
}
|
Loading…
Reference in new issue