# 堆-优先队列进阶：TopK-3D接雨水-C-Js-Rust语言描述

## 1 前言

1. 数据与结构与算法: 堆 C语言描述

2. 数据结构与算法: 堆 优先队列 JavaScript语言描述

## 2 `TopK`问题-堆解法

### 2.2 `C`语言描述

``````typedef struct MinHeap {
int *data; //数据域
int size; //长度
int max;//最大长度
} MinHeap;
void insert(struct MinHeap *p, int item){
if (p->size == p->max)
{
printf("******:\n");
return;
}
int i = ++p->size;
for (; p->data[i/2] > item; i/=2)
{
if (i <= 1)
{
break;
}
p->data[i] = p->data[i/2];
}
p->data[i] = item;
}
void delete(struct MinHeap *p){
p->data[1] = p->data[p->size--];
int i = 1;
int tmp;
while(2*i <= p->size){
//如果有右节点
if (2*i + 1 <= p->size)
{
if (p->data[2*i + 1] < p->data[i] || p->data[2*i] < p->data[i])
{
if (p->data[2*i + 1] < p->data[2*i])
{
tmp              = p->data[i];
p->data[i]       = p->data[2*i + 1];
p->data[2*i + 1] = tmp;
i          = 2*i + 1;
}else{
tmp              = p->data[i];
p->data[i]       = p->data[2*i];
p->data[2*i] = tmp;
i          = 2*i;
}
}else{
return;
}

}else{
//只有左节点
if (p->data[2*i] < p->data[i])
{
tmp              = p->data[i];
p->data[i]       = p->data[2*i];
p->data[2*i] = tmp;
i                  = 2*i;
}else{
return;
}
}
}
}

int findKthLargest(int* nums, int numsSize, int k){
struct MinHeap *heap;
heap=(struct MinHeap *)malloc(sizeof(struct MinHeap));
if (heap == NULL)
{
printf("malloc error \n");
return 0;
}
heap->size = 0;
heap->max  = k + 1;
heap->data = (int*)malloc(sizeof(int)*heap->max);
if(heap->data == NULL)
{
printf("malloc error \n");
return 0;
}
heap->data[0] = 9999;
for (int i = 0; i < numsSize; ++i)
{
if(i < k){
insert(heap, nums[i]);
}else{
if(nums[i] > heap->data[1]){
delete(heap);
insert(heap, nums[i]);
}
}
}
return heap->data[1];
}``````

## 3 `3D`接雨水-优先队列解法

### 3.1 思路

## 3.2 `Js`语言描述

`````` var Heap = function () {
this.data = []
this.insert = (obj) => {
let i = this.data.length
for (; i > 0 && this.data[Math.floor((i - 1) / 2)].val >= obj.val; i = Math.floor((i - 1) / 2)) {
if (i < 1) {
break
}
this.data[i] = this.data[Math.floor((i - 1) / 2)]
}
this.data[i] = obj
}
this.pop = () => {
if (this.data.length == 0) {
return null
}
if (this.data.length == 1) {
return this.data.pop()
}
let top = this.data[0]
this.data[0] = this.data.pop()

let i = 0
while (2 * i + 1 < this.data.length) {
if (2 * i + 2 < this.data.length) {
if (this.data[2 * i + 2].val < this.data[i].val || this.data[2 * i + 1].val < this.data[i].val) {
if (this.data[2 * i + 2].val < this.data[2 * i + 1].val) {
this.swap(i, 2 * i + 2)
i = 2 * i + 2
} else {
this.swap(i, 2 * i + 1)
i = 2 * i + 1
}
} else {
break
}
} else {
if (this.data[2 * i + 1].val < this.data[i].val) {
this.swap(i, 2 * i + 1)
i = 2 * i + 1
} else {
break
}
}
}
}
this.swap = (i, j) => {
let tmp = this.data[j]
this.data[j] = this.data[i]
this.data[i] = tmp
}
};``````

### 3.3 `Rust`语言描述

``````use std::cmp::Ordering;
use std::collections::BinaryHeap;

#[derive(Copy, Clone, Eq, PartialEq)]
struct Item {
val: i32,
i: usize,
j: usize,
}

impl Ord for Item {
fn cmp(&self, other: &Self) -> Ordering {
other.val.cmp(&self.val)
}
}

impl PartialOrd for Item {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Solution {
pub fn trap_rain_water(height_map: Vec<Vec<i32>>) -> i32 {
let h = height_map.len();
let w = height_map[0].len();
if h <= 2 || w <= 2 {
return 0;
}

let mut water = vec![vec![-1; w]; h];
let mut pq: BinaryHeap<Item> = BinaryHeap::new();

let mut i = 0;
while i < h {
water[i][0]     = height_map[i][0];
water[i][w - 1] = height_map[i][w - 1];
pq.push(Item {
val: height_map[i][0],
i: i,
j: 0,
});
pq.push(Item {
val: height_map[i][w - 1],
i: i,
j: w - 1,
});
i += 1;
}
i = 1;
while i < w - 1 {
water[0][i]         = height_map[0][i];
water[h - 1][i]     = height_map[h - 1][i];
pq.push(Item {
val: height_map[0][i],
i: 0,
j: i,
});
pq.push(Item {
val: height_map[h - 1][i],
i: h - 1,
j: i
});
i += 1;
}
let dirs :[i32;5] = [-1, 0, 1, 0, -1];
let mut res = 0;
let mut k;
while pq.len() > 0 {
if let Some(Item { val, i, j }) = pq.pop() {
k = 0;
while k < 4 {
let nx = (i as i32 + dirs[k])   as usize;
let ny = (j as i32 + dirs[k+1]) as usize;
if 0 < nx && nx < h as usize && 0 < ny && ny < w && water[nx][ny] == -1 {
if height_map[nx][ny]  < val {
res += val - height_map[nx][ny];
}
water[nx][ny] = val;
pq.push(Item {
val: val.max(height_map[nx][ny]),
i: nx,
j: ny
});
}
k += 1;
}
}
}
return res;
}
}``````

##### tfzh

code what u love & love what u code

212 声望
15 粉丝
0 条评论