Time: 10 minutes

A simple question shouldn’t ask me to write the maximum heap by hand.

const swap = function (arr,i,j) {
    [arr[i],arr[j]] = [arr[j],arr[i]]
}
class MaxHeap {
    constructor() {
        this.count = 0        this.data = new Array(this.count + 1)
    }
    shiftUp(k) {
        // Put the new elements up while(k>=1) {
            let father = Math.floor(k / 2)
            if (this.data[k] > this.data[father]) {
                swap(this.data,k,father)
                k = father
            } else {
                break            }
        }
    }
    shiftDown(k) {
        while( k * 2  <= this.count) { // Indicates that k has children let j = k
            if (k * 2 + 1 <= this.count && this.data[k * 2 + 1] > this.data[k] && this.data[k * 2 + 1] > this.data[k * 2]) {
                j = k * 2 + 1            } else if (this.data[k * 2] > this.data[k]) {
                j = k * 2            } else {
                break            }
            swap(this.data,j,k)
            k = j
        }
    }
    size() {
        return this.count    }
    isEmpty() {
        return this.count === 0    }
    insert(item) {
        this.data[++this.count] = item
        this.shiftUp(this.count)
    }
    extractMax() {
        if (this.count < 0) return
        let ret = this.data[1]
        swap(this.data,1,this.count--)
        this.shiftDown(1)
        return ret
    }
}
var lastStoneWeight = function(stones) {
    var heap = new MaxHeap()
    for(let i = 0 ; i < stones.length ; i++) {
        heap.insert(stones[i])
    }
    while(heap.size() > 1) {
        var s1 = heap.extractMax()
        var s2 = heap.extractMax()
        var reduce = Math.abs(s1 - s2)
        if (reduce) {
            heap.insert(reduce)
        }
    }
    if (heap.size() === 0) {
        return 0    }
    return heap.extractMax()
};

Time: 10 minutes

A simple question shouldn’t ask me to write the maximum heap by hand.

const swap = function (arr,i,j) {
    [arr[i],arr[j]] = [arr[j],arr[i]]
}
class MaxHeap {
    constructor() {
        this.count = 0        this.data = new Array(this.count + 1)
    }
    shiftUp(k) {
        // Put the new elements up while(k>=1) {
            let father = Math.floor(k / 2)
            if (this.data[k] > this.data[father]) {
                swap(this.data,k,father)
                k = father
            } else {
                break            }
        }
    }
    shiftDown(k) {
        while( k * 2  <= this.count) { // Indicates that k has children let j = k
            if (k * 2 + 1 <= this.count && this.data[k * 2 + 1] > this.data[k] && this.data[k * 2 + 1] > this.data[k * 2]) {
                j = k * 2 + 1            } else if (this.data[k * 2] > this.data[k]) {
                j = k * 2            } else {
                break            }
            swap(this.data,j,k)
            k = j
        }
    }
    size() {
        return this.count    }
    isEmpty() {
        return this.count === 0    }
    insert(item) {
        this.data[++this.count] = item
        this.shiftUp(this.count)
    }
    extractMax() {
        if (this.count < 0) return
        let ret = this.data[1]
        swap(this.data,1,this.count--)
        this.shiftDown(1)
        return ret
    }
}
var lastStoneWeight = function(stones) {
    var heap = new MaxHeap()
    for(let i = 0 ; i < stones.length ; i++) {
        heap.insert(stones[i])
    }
    while(heap.size() > 1) {
        var s1 = heap.extractMax()
        var s2 = heap.extractMax()
        var reduce = Math.abs(s1 - s2)
        if (reduce) {
            heap.insert(reduce)
        }
    }
    if (heap.size() === 0) {
        return 0    }
    return heap.extractMax()
};