当前位置:网站首页>Leetcode heap correlation
Leetcode heap correlation
2022-07-05 06:13:00 【Dawnlighttt】
List of articles
q215 The... In the array k The biggest element
Answer key
Although the title requires that the number in the array be returned k The biggest element , But what I build is a small root pile , Because when building a small root heap , Assigning values to the result array starts from large to small , That is, assign values from the end of the array . So when the last sorting operation is traversed , When len(nums) - i == k when , Go straight back to nums[i] that will do .
func findKthLargest(nums []int, k int) int {
// Building the heap
for i := len(nums) / 2 - 1; i >= 0; i-- {
heapify(nums, len(nums), i)
}
for i := len(nums) - 1; i > 0; i-- {
nums[i], nums[0] = nums[0], nums[i]
heapify(nums, i, 0)
if len(nums) - i == k {
return nums[i]
}
}
return nums[0]
}
func heapify(arr []int, n int, i int) {
// Heap maintenance
largest := i //i Is the subscript of the current parent node
lson := i * 2 + 1 // Left the child
rson := i * 2 + 2 // The right child
// Save the subscript of the maximum value
if lson < n && arr[largest] < arr[lson] {
largest = lson
}
if rson < n && arr[largest] < arr[rson] {
largest = rson
}
if largest != i {
arr[largest], arr[i] = arr[i], arr[largest]
// Recursively maintain the heap to the child node
heapify(arr, n, largest)
}
}
q347 front k High frequency elements
Answer key
First, use one hash Table to record the number of times each number appears , Then open a two-dimensional array ,count[i][0] Used to store the elements of a given array ,count[i][1] Used to store the number of occurrences of a given array element . Then use heap sorting , Sort the elements according to the number of occurrences , According to count[i][1] To build a small root pile , In order to count Array to sort . And then count[i][0] Deposit in res in , Finally, when len(nums) - i == k when , return res that will do . The practice is similar to the previous question .
func topKFrequent(nums []int, k int) []int {
hashTable := make(map[int]int)
for _, v := range nums {
hashTable[v]++
}
var count [][]int
for k, v := range hashTable {
count = append(count, []int{
k, v})
}
res := make([]int, 0)
// Building the heap
for i := len(count) / 2 - 1; i >= 0; i-- {
heapify(count, len(count), i)
}
for i := len(count) - 1; i >= 0; i-- {
count[i], count[0] = count[0], count[i]
heapify(count, i, 0)
res = append(res, count[i][0])
if len(count) - i == k {
break
}
}
return res
}
func heapify(arr [][]int, n int, i int) {
// Heap maintenance
largest := i //i Is the subscript of the current parent node
lson := i * 2 + 1 // Left the child
rson := i * 2 + 2 // The right child
// Save the subscript of the maximum value
if lson < n && arr[largest][1] < arr[lson][1] {
largest = lson
}
if rson < n && arr[largest][1] < arr[rson][1] {
largest = rson
}
if largest != i {
arr[largest], arr[i] = arr[i], arr[largest]
// Recursively maintain the heap to the child node
heapify(arr, n, largest)
}
}
边栏推荐
- 6. Logistic model
- 927. 三等分 模拟
- Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
- 1.14 - assembly line
- [rust notes] 15 string and text (Part 1)
- Navicat连接Oracle数据库报错ORA-28547或ORA-03135
- 1039 Course List for Student
- Common optimization methods
- Basic explanation of typescript
- One question per day 1765 The highest point in the map
猜你喜欢

1.13 - RISC/CISC

LeetCode 0107. Sequence traversal of binary tree II - another method

wordpress切换页面,域名变回了IP地址

Groupbykey() and reducebykey() and combinebykey() in spark

Sqlmap tutorial (1)

快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
![[practical skills] how to do a good job in technical training?](/img/a3/7a1564cd9eb564abfd716fef08a9e7.jpg)
[practical skills] how to do a good job in technical training?

7. Processing the input of multidimensional features

Redis publish subscribe command line implementation

Introduction et expérience de wazuh open source host Security Solution
随机推荐
Dynamic planning solution ideas and summary (30000 words)
MIT-6874-Deep Learning in the Life Sciences Week 7
【Rust 笔记】17-并发(下)
数据可视化图表总结(一)
Simple knapsack, queue and stack with deque
[rust notes] 13 iterator (Part 2)
Solution to game 10 of the personal field
1041 Be Unique
Sword finger offer II 058: schedule
Quickly use Amazon memorydb and build your own redis memory database
【Rust 笔记】15-字符串与文本(下)
Full Permutation Code (recursive writing)
[rust notes] 14 set (Part 1)
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
Leetcode-6108: decrypt messages
Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
Appium自动化测试基础 — Appium测试环境搭建总结
2022 极术通讯-Arm 虚拟硬件加速物联网软件开发
MIT-6874-Deep Learning in the Life Sciences Week 7
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章