-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1102-path-with-maximum-minimum-value.js
More file actions
47 lines (40 loc) · 1.37 KB
/
1102-path-with-maximum-minimum-value.js
File metadata and controls
47 lines (40 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 1102. Path With Maximum Minimum Value
* https://leetcode.com/problems/path-with-maximum-minimum-value/
* Difficulty: Medium
*
* Given an m x n integer matrix grid, return the maximum score of a path starting
* at (0, 0) and ending at (m - 1, n - 1) moving in the 4 cardinal directions.
*
* The score of a path is the minimum value in that path.
* - For example, the score of the path 8 → 4 → 5 → 9 is 4.
*/
/**
* @param {number[][]} grid
* @return {number}
*/
var maximumMinimumPath = function(grid) {
const m = grid.length;
const n = grid[0].length;
const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];
const visited = new Array(m).fill(null).map(() => new Array(n).fill(false));
const maxHeap = new PriorityQueue((a, b) => b[0] - a[0]);
maxHeap.enqueue([grid[0][0], 0, 0]);
visited[0][0] = true;
while (!maxHeap.isEmpty()) {
const [currentScore, row, col] = maxHeap.dequeue();
if (row === m - 1 && col === n - 1) {
return currentScore;
}
for (const [dr, dc] of directions) {
const newRow = row + dr;
const newCol = col + dc;
if (newRow >= 0 && newRow < m && newCol >= 0 && newCol < n && !visited[newRow][newCol]) {
visited[newRow][newCol] = true;
const newScore = Math.min(currentScore, grid[newRow][newCol]);
maxHeap.enqueue([newScore, newRow, newCol]);
}
}
}
return -1;
};