LeetCode 1631. Path With Minimum 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 }

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/778414.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

javaCC链3

cc3 cc3区别cc6不再是使用Rutime类执行命令,而是通过类加载器动态加载恶意类然后执行 类加载: ClassLoader.loadClass->ClassLoader.findClass->ClassLLoader.defineClass ClassLoader.loadClass:寻找加载的类(双亲委派机制) ClassLoader.findClass:如果loadClass没…

数据结构 顺序与链式二叉树的原理与实现(万字)

目录一、树概念及结构树的概念树的相关概念树的表示二、二叉树概念及结构概念特殊的二叉树二叉树的性质二叉树的存储结构三、二叉树的顺序结构及实现二叉树的顺序结构堆的概念及结构堆的实现堆向下调整算法堆的创建建堆时间复杂度堆的插入堆的删除堆的代码实现Heap.hHeap.c堆的…

Logisim-009-4位并行加载寄存器

仓库地址 https://gitee.com/gitliang/logisim-to-cpu

CTF—web专项

一:信息泄露 1、目录遍历漏洞 (1)原理:本质是没有过滤用户输入的 ../ 相关的目录跳转符,使得攻击者通过目录跳转符来遍历服务器中的任意文件。 (2)题解: eg:根据提示遍历网页目录信息,会在某一个文件夹中发现一个flag.txt文件2、phpinfo泄露 (1)定义:phpinfo 是 PH…

算术运算符里的特殊运算符

1.++(自加) 给一个数加1 自加分为两种,一个是++a,另一个是a++ a++:先给数赋值,然后再给a加1++a:先给a加1,然后再给数赋值2.--(自减) 给一个数减1

中文手写体识别(ocr)测试

记录一下,以下是测试中文手写体识别结果图展示(对于潦草的字迹效果一般),后期会开放模型,有java和python版本:

【Playwright+Python】系列教程(七)使用Playwright进行API接口测试

playwright也是可以做接口测试的,但个人觉得还是没有requests库强大,但和selenium相比的话,略胜一筹,毕竟支持API登录,也就是说可以不用交互直接调用接口操作了。 怎么用 既然是API的测试了,那肯定就别搞UI自动化那套,搞什么浏览器交互,那叫啥API测试,纯属扯淡。 也不…

继承和多态

继承 继承: 继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类。 继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例 域和方法,或子类从父类继承方法,使得子类具有父类相同的行为。 生活中的继承:继承的概述:继承是面向对象程序设…

数据的存储和排列

大端方式和小端方式边界对齐

中位数

题目题解1. 首先我们可以想到,既然需要输出(n+1)/2次,所以我们可以每次排序一下,并在其为奇数的时候输出它们中间的数。 #include<iostream> #include<algorithm> #include<queue> using namespace std; int N; int a[100001]; int main() { cin >&g…

Unity Gyro Camera ---- 传感器控制摄像头旋转 + 正北校准 (纯原生支持Android+IOS,无需安装ARKit,ARCore等插件)

Unity Gyro Camera 传感器控制摄像头旋转 + 正北校准 纯原生支持Android+IOS,无需安装ARKit,ARCore等插件这篇文章主要介绍如何利用手机原生的传感器,控制摄像头的旋转,最终可以实现AR或者VR的摄像头旋转控制问题提出 虽然,目前有一些用手机传感器控制虚拟摄像头旋转的方案…

浮点数的表示及IEEE754标准

浮点数的表示浮点数的规格化IEEE754标准 移码IEEE754这里有一个需要特别注意的地方,IEEE754中,尾数个位上的1是隐含的IEEE 的阶码保留了全0和全1来表示特殊的状态,所以阶码最大值的真值为127,对应机器数为1111 1110,阶码最小值的真值为-126,对应的机器数为0000 0001