1631. Path With Minimum Effort Medium
#Array#Binary Search#Depth-First Search#Breadth-First Search#Union Find#Heap (Priority Queue)#Matrix
1/**
2 * [1631] Path With Minimum Effort
3 *
4 * You are a hiker preparing for an upcoming hike. You are given heights, a 2D array of size rows x columns, where heights[row][col] represents the height of cell (row, col). You are situated in the top-left cell, (0, 0), and you hope to travel to the bottom-right cell, (rows-1, columns-1) (i.e., 0-indexed). You can move up, down, left, or right, and you wish to find a route that requires the minimum effort.
5 *
6 * A route's effort is the maximum absolute difference in heights between two consecutive cells of the route.
7 *
8 * Return the minimum effort required to travel from the top-left cell to the bottom-right cell.
9 *
10 *
11 * Example 1:
12 *
13 * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex1.png" style="width: 300px; height: 300px;" />
14 *
15 *
16 * Input: heights = [[1,2,2],[3,8,2],[5,3,5]]
17 * Output: 2
18 * Explanation: The route of [1,3,5,3,5] has a maximum absolute difference of 2 in consecutive cells.
19 * This is better than the route of [1,2,2,2,5], where the maximum absolute difference is 3.
20 *
21 *
22 * Example 2:
23 *
24 * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex2.png" style="width: 300px; height: 300px;" />
25 *
26 *
27 * Input: heights = [[1,2,3],[3,8,4],[5,3,5]]
28 * Output: 1
29 * Explanation: The route of [1,2,3,4,5] has a maximum absolute difference of 1 in consecutive cells, which is better than route [1,3,5,3,5].
30 *
31 *
32 * Example 3:
33 * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex3.png" style="width: 300px; height: 300px;" />
34 *
35 * Input: heights = [[1,2,1,1,1],[1,2,1,2,1],[1,2,1,2,1],[1,2,1,2,1],[1,1,1,2,1]]
36 * Output: 0
37 * Explanation: This route does not require any effort.
38 *
39 *
40 *
41 * Constraints:
42 *
43 *
44 * rows == heights.length
45 * columns == heights[i].length
46 * 1 <= rows, columns <= 100
47 * 1 <= heights[i][j] <= 10^6
48 *
49 */
50pub struct Solution {}
51
52// problem: https://leetcode.com/problems/path-with-minimum-effort/
53// discuss: https://leetcode.com/problems/path-with-minimum-effort/discuss/?currentPage=1&orderBy=most_votes&query=
54
55// submission codes start here
56
57impl Solution {
58 pub fn minimum_effort_path(heights: Vec<Vec<i32>>) -> i32 {
59
60 }
61}
62
63// submission codes end
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[test]
70 fn test_1631() {
71 }
72}
73
Back
© 2025 bowen.ge All Rights Reserved.