Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit afc1fc6

Browse files
authored
Create 1046-Last-Stone-Weight.js
1 parent 32855f0 commit afc1fc6

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

javascript/1046-Last-Stone-Weight.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
class Heap {
2+
constructor(stones){
3+
this.heap = stones
4+
this.size = stones.length
5+
this.heapify(0)
6+
}
7+
right(pos) {
8+
return 2*pos + 2
9+
}
10+
left(pos) {
11+
return 2*pos + 1
12+
}
13+
isleaf(pos) {
14+
if (2*pos + 1 >= this.size) return true
15+
return false
16+
}
17+
swap(a,b){
18+
let temp = this.heap[a]
19+
this.heap[a] = this.heap[b]
20+
this.heap[b] = temp
21+
}
22+
fix(pos){
23+
if (this.isleaf(pos)) return
24+
let left = this.left(pos)
25+
let right = this.right(pos)
26+
let bigger = left
27+
if (right < this.size) bigger = this.heap[left] > this.heap[right] ? left : right
28+
if (this.heap[pos] < this.heap[bigger]) {
29+
this.swap(pos,bigger)
30+
this.fix(bigger)
31+
}
32+
}
33+
heapify(pos){
34+
if (this.isleaf(pos)) return
35+
this.heapify(this.left(pos))
36+
this.heapify(this.right(pos))
37+
this.fix(pos)
38+
}
39+
delete(){
40+
this.swap(0, --this.size)
41+
this.fix(0)
42+
return this.heap[0]
43+
}
44+
insert(val){
45+
this.size++
46+
this.heap[this.size-1] = val
47+
this.heapify(0)
48+
}
49+
peek(){
50+
return this.heap[0]
51+
}
52+
}
53+
/**
54+
* @param {number[]} stones
55+
* @return {number}
56+
*/
57+
var lastStoneWeight = function(stones) {
58+
//use a max heap to pop off the top 2 stones at each time
59+
//if result is 0 we can do nothing
60+
//if the result is a new weight we can push it back to the heap
61+
const heap = new Heap(stones)
62+
while(heap.size > 1) {
63+
let x = heap.peek()
64+
heap.delete()
65+
let y = heap.peek()
66+
heap.delete()
67+
const res = x-y
68+
if (res > 0) heap.insert(res)
69+
}
70+
if (heap.size) return heap.peek()
71+
return 0
72+
};

0 commit comments

Comments
 (0)