首页 > 其他分享 >LeetCode 1631. Path With Minimum Effort

LeetCode 1631. Path With Minimum Effort

时间:2024-08-05 23:05:41浏览次数:17  
标签:1631 cur int route heights length Minimum minHeap Effort

原题链接在这里:https://leetcode.com/problems/path-with-minimum-effort/description/

题目:

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.

A route's effort is the maximum absolute difference in heights between two consecutive cells of the route.

Return the minimum effort required to travel from the top-left cell to the bottom-right cell.

Example 1:

Input: heights = [[1,2,2],[3,8,2],[5,3,5]]
Output: 2
Explanation: The route of [1,3,5,3,5] has a maximum absolute difference of 2 in consecutive cells.
This is better than the route of [1,2,2,2,5], where the maximum absolute difference is 3.

Example 2:

Input: heights = [[1,2,3],[3,8,4],[5,3,5]]
Output: 1
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].

Example 3:

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]]
Output: 0
Explanation: This route does not require any effort.

Constraints:

  • rows == heights.length
  • columns == heights[i].length
  • 1 <= rows, columns <= 100
  • 1 <= heights[i][j] <= 106

题解:

Use minHeap to keep track of (x, y, diff), sorted based on diff.

When polling from minHeap, if it reaches the bottom right, then return the diff.

Otherwise, check all four directions and if not visited before, add to minHeap.

Time Complexity: O(m * n * log(m * n)). m = heights.length. n = heights[0].length.

Space: O(m * n).

AC Java:

 1 class Solution {
 2     int[][] dirs = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
 3     public int minimumEffortPath(int[][] heights) {
 4         int m = heights.length;
 5         int n = heights[0].length;
 6 
 7         boolean[][] visited = new boolean[m][n];
 8         PriorityQueue<int[]> minHeap = new PriorityQueue<>((a, b) -> a[2] - b[2]);
 9         minHeap.add(new int[]{0, 0, 0});
10         while(!minHeap.isEmpty()){
11             int[] cur = minHeap.poll();
12             if(cur[0] == m - 1 && cur[1] == n - 1){
13                 return cur[2];
14             }
15 
16             visited[cur[0]][cur[1]] = true;
17 
18             for(int [] dir : dirs){
19                 int x = cur[0] + dir[0];
20                 int y = cur[1] + dir[1];
21                 if(x < 0 || x >= m || y < 0 || y >= n || visited[x][y]){
22                     continue;
23                 }
24 
25                 int diff = Math.abs(heights[x][y] - heights[cur[0]][cur[1]]);
26                 minHeap.add(new int[]{x, y, Math.max(cur[2], diff)});
27             }
28         }
29 
30         return 0;
31     }
32 }

 

标签:1631,cur,int,route,heights,length,Minimum,minHeap,Effort
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18344213

相关文章

  • LeetCode | 209 MinimumSizeSubarraySum
    分析本题中是找与target相符的最小连续的子数组和,找一个能够容纳target这么大的最小子区间。虽然本节引入了滑动窗口的概念,可我更偏好于,这是一只毛毛虫在数组上的移动,找到最小的容纳自己位置的地方target可看作毛虫本身的重量,数组中的每个元素值表示可承受的重量,right右指针看......
  • LeetCode 2976 Minimum Cost to Convert String I
    MinimumCosttoConvertStringIProblemDescriptionYouaregiventwo0-indexedstrings,sourceandtarget,bothoflengthnandconsistingoflowercaseEnglishletters.Youarealsoprovidedwithtwo0-indexedcharacterarrays,originalandchanged,a......
  • F. Minimum Maximum Distance
    原题链接题解1.假设有一个以标记点\(c\)为根的子树,且子树内没有其他标记点,易得该子树内所有点的\(f\leqf(c)\),所以我们可以把该子树内的非标记点全部删掉2.完成步骤1之后,图就变成了所有叶子节点均为标记点的树3.题目等价于求该树内,最小的点到边界的最大值,也就是求树的直径......
  • Minimum_jerk参考代码
    1.参考代码importnumpyasnpimportmatplotlib.pyplotaspltfromcvxoptimportmatrix,solversdefgenQk(T_down,T_up):Q=np.zeros((6,6))Q[3][4]=72*(T_up**2-T_down**2)Q[3][5]=120*(T_up**3-T_down**3)Q[4][5]=360*(T_up*......
  • LeetCode 857. Minimum Cost to Hire K Workers
    原题链接在这里:https://leetcode.com/problems/minimum-cost-to-hire-k-workers/description/题目:Thereare n workers.Youaregiventwointegerarrays quality and wage where quality[i] isthequalityofthe ith workerand wage[i] istheminimumwagee......
  • Leetcode 3203. Find Minimum Diameter After Merging Two Trees
    Leetcode3203.FindMinimumDiameterAfterMergingTwoTrees1.解题思路2.代码实现题目链接:3203.FindMinimumDiameterAfterMergingTwoTrees1.解题思路这一题的话算是一个拓扑树的题目?总之就是从树的叶子节点不断向上遍历,不断地删除已访问的叶子节点,并加入......
  • LeetCode 2268. Minimum Number of Keypresses
    原题链接在这里:https://leetcode.com/problems/minimum-number-of-keypresses/description/题目:Youhaveakeypadwith 9 buttons,numberedfrom 1 to 9,eachmappedtolowercaseEnglishletters.Youcanchoosewhichcharacterseachbuttonismatchedtoaslong......
  • LeetCode 2340. Minimum Adjacent Swaps to Make a Valid Array
    原题链接在这里:https://leetcode.com/problems/minimum-adjacent-swaps-to-make-a-valid-array/description/题目:Youaregivena 0-indexed integerarray nums.Swaps of adjacent elementsareabletobeperformedon nums.A valid arraymeetsthefollowingco......
  • 209. Minimum Size Subarray Sum
    Givenanarrayofpositiveintegersnumsandapositiveintegertarget,returntheminimallengthofasubarraywhosesumisgreaterthanorequaltotarget.Ifthereisnosuchsubarray,return0instead.Example1:Input:target=7,nums=[2,3,1,2,......
  • P9327 [CCC 2023 S4] Minimum Cost Roads
    原题链接题解贪心,我管这种叫做策略贪心,即按照某种顺序或者角度去贪心可以得到最优解既然题目要求任意两点间最短路最小的同时,价格也最小,那么我们就按长度为第一关键字,花费为第二关键字排序。然后遍历所有边看看这条边能否使用遍历过程的策略:如果这条边加入后,这条边两端的节点......