-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 94 ms (85.18%) | Memory: 67 MB (38.89%) - LeetSync
- Loading branch information
1 parent
fa60def
commit df41c59
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
1762-furthest-building-you-can-reach/furthest-building-you-can-reach.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* @param {number[]} heights | ||
* @param {number} bricks | ||
* @param {number} ladders | ||
* @return {number} | ||
*/ | ||
var furthestBuilding = function(h, b, l) { | ||
const p = new MaxHeap(); | ||
|
||
let i = 0, diff = 0; | ||
for (i = 0; i < h.length - 1; i++) { | ||
diff = h[i + 1] - h[i]; | ||
|
||
if (diff <= 0) { | ||
continue; | ||
} | ||
|
||
b -= diff; | ||
p.push(diff); | ||
|
||
if (b < 0) { | ||
b += p.pop(); | ||
l--; | ||
} | ||
|
||
if (l < 0) { | ||
break; | ||
} | ||
} | ||
|
||
return i; | ||
}; | ||
|
||
class MaxHeap { | ||
constructor() { | ||
this.heap = []; | ||
} | ||
|
||
push(val) { | ||
this.heap.push(val); | ||
this.heapifyUp(); | ||
} | ||
|
||
pop() { | ||
const max = this.heap[0]; | ||
const last = this.heap.pop(); | ||
|
||
if (this.heap.length > 0) { | ||
this.heap[0] = last; | ||
this.heapifyDown(); | ||
} | ||
|
||
return max; | ||
} | ||
|
||
heapifyUp() { | ||
let current = this.heap.length - 1; | ||
while (current > 0) { | ||
const parent = Math.floor((current - 1) / 2); | ||
if (this.heap[parent] >= this.heap[current]) { | ||
break; | ||
} | ||
[this.heap[parent], this.heap[current]] = [this.heap[current], this.heap[parent]]; | ||
current = parent; | ||
} | ||
} | ||
|
||
heapifyDown() { | ||
let current = 0; | ||
while (true) { | ||
let leftChild = 2 * current + 1; | ||
let rightChild = 2 * current + 2; | ||
let maxChild = leftChild; | ||
|
||
if (rightChild < this.heap.length && this.heap[rightChild] > this.heap[leftChild]) { | ||
maxChild = rightChild; | ||
} | ||
|
||
if (leftChild >= this.heap.length || this.heap[current] >= this.heap[maxChild]) { | ||
break; | ||
} | ||
|
||
[this.heap[current], this.heap[maxChild]] = [this.heap[maxChild], this.heap[current]]; | ||
current = maxChild; | ||
} | ||
} | ||
} | ||
|