2
* @param {number[][]} heights7
var minimumEffortPath = function (heights) {14
const row = heights.length;15
const col = heights[0].length;16
const differences = [];17
for (let i = 0; i < row; i++) {18
for (let j = 0; j < col; j++) {19
if (!differences[i]) {20
differences[i] = [Infinity];22
differences[i].push(Infinity);26
differences[0][0] = 0;27
const pq = new PriorityQueue();29
while (pq.data.length > 0) {30
const node = pq.shift();31
const difference = node.priority;32
const [x, y] = node.val;33
directions.forEach(([dx, dy]) => {36
if (newX >= 0 && newX < row && newY >= 0 && newY < col) {37
const currentDiff = Math.abs(heights[newX][newY] - heights[x][y]);38
const maxDiff = Math.max(currentDiff, differences[x][y]);39
if (differences[newX][newY] > maxDiff) {40
differences[newX][newY] = maxDiff;41
pq.push([newX, newY], maxDiff);46
return differences[row - 1][col - 1];49
const swap = (arr, i, j) => {55
function Node(val, priority) {57
this.priority = priority;60
function PriorityQueue() {64
PriorityQueue.prototype.push = function push(val, priority) {65
const node = new Node(val, priority);67
let index = this.data.length - 1;69
const parentIndex = Math.floor((index - 1) / 2);70
const parent = this.data[parentIndex];71
if (parent.priority > node.priority) {72
swap(this.data, parentIndex, index);80
PriorityQueue.prototype.shift = function shift() {81
const minNode = this.data[0] || {};82
const lastNode = this.data.pop();83
if (this.data.length < 1) {86
this.data[0] = lastNode;88
while (index < this.data.length) {89
const leftIndex = 2 * index + 1;90
const rightIndex = 2 * index + 2;91
const leftNode = this.data[leftIndex] || {};92
const rightNode = this.data[rightIndex] || {};94
if (leftNode.priority < lastNode.priority) {95
smallerIndex = leftIndex;97
if (!smallerIndex && rightNode.priority < lastNode.priority) {98
smallerIndex = rightIndex;100
if (smallerIndex && rightNode.priority < leftNode.priority) {101
smallerIndex = rightIndex;106
swap(this.data, index, smallerIndex);107
index = smallerIndex;