Skip to content

Commit 1ddec91

Browse files
committedApr 23, 2025
Add solution #1631
1 parent fd2d0c1 commit 1ddec91

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,434 LeetCode solutions in JavaScript
1+
# 1,435 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1258,6 +1258,7 @@
12581258
1627|[Graph Connectivity With Threshold](./solutions/1627-graph-connectivity-with-threshold.js)|Hard|
12591259
1629|[Slowest Key](./solutions/1629-slowest-key.js)|Easy|
12601260
1630|[Arithmetic Subarrays](./solutions/1630-arithmetic-subarrays.js)|Medium|
1261+
1631|[Path With Minimum Effort](./solutions/1631-path-with-minimum-effort.js)|Medium|
12611262
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12621263
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12631264
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* 1631. Path With Minimum Effort
3+
* https://leetcode.com/problems/path-with-minimum-effort/
4+
* Difficulty: Medium
5+
*
6+
* You are a hiker preparing for an upcoming hike. You are given heights, a 2D array of size
7+
* rows x columns, where heights[row][col] represents the height of cell (row, col). You are
8+
* situated in the top-left cell, (0, 0), and you hope to travel to the bottom-right cell,
9+
* (rows-1, columns-1) (i.e., 0-indexed). You can move up, down, left, or right, and you wish
10+
* to find a route that requires the minimum effort.
11+
*
12+
* A route's effort is the maximum absolute difference in heights between two consecutive
13+
* cells of the route.
14+
*
15+
* Return the minimum effort required to travel from the top-left cell to the bottom-right cell.
16+
*/
17+
18+
/**
19+
* @param {number[][]} heights
20+
* @return {number}
21+
*/
22+
var minimumEffortPath = function(heights) {
23+
const rows = heights.length;
24+
const cols = heights[0].length;
25+
const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];
26+
27+
function canReach(maxEffort) {
28+
const visited = Array.from({ length: rows }, () => Array(cols).fill(false));
29+
const queue = [[0, 0]];
30+
visited[0][0] = true;
31+
32+
while (queue.length) {
33+
const [row, col] = queue.shift();
34+
if (row === rows - 1 && col === cols - 1) return true;
35+
36+
for (const [dr, dc] of directions) {
37+
const newRow = row + dr;
38+
const newCol = col + dc;
39+
40+
if (newRow >= 0 && newRow < rows && newCol >= 0
41+
&& newCol < cols && !visited[newRow][newCol]) {
42+
const effort = Math.abs(heights[newRow][newCol] - heights[row][col]);
43+
if (effort <= maxEffort) {
44+
visited[newRow][newCol] = true;
45+
queue.push([newRow, newCol]);
46+
}
47+
}
48+
}
49+
}
50+
return false;
51+
}
52+
53+
let left = 0;
54+
let right = 1000000;
55+
let result = right;
56+
57+
while (left <= right) {
58+
const mid = Math.floor((left + right) / 2);
59+
if (canReach(mid)) {
60+
result = mid;
61+
right = mid - 1;
62+
} else {
63+
left = mid + 1;
64+
}
65+
}
66+
67+
return result;
68+
};

0 commit comments

Comments
 (0)
Please sign in to comment.