Leetcode 45. Jump Game II
来源:互联网 发布:vmware mac win10镜像 编辑:程序博客网 时间:2024/05/16 08:42
BFS solution for reference.
/** * Breadth first search o(n). * Map each number in the array to a level, * where numbers in level i are all the numbers that can be reached in i-1th jump. * e.g. 2, 3, 1, 1, 4 * level 0: 2 * level 1: 3, 1 * level 2: 1, 4 * The minimum jump is 2. */ public class Solution { public int jump(int[] nums) { if (nums.length < 2) return 0; int i = 1, lvl = 1, maxReach = nums[0], nextMax = 0; while (maxReach < nums.length-1) { for (; i<=maxReach; i++) // find the max reach of a level // there always exists a i+nums[i] >= maxReach (*) // so we can use nextMax to record the maxReach nextMax = Math.max(i+nums[i], nextMax); // maxReach doesn't move forward after scanning a level // which means we can never jump to the end if (maxReach == nextMax) return Integer.MAX_VALUE; maxReach = nextMax; lvl++; } return lvl; }}
0 0
- LeetCode --- 45. Jump Game II
- LeetCode 45.Jump Game II
- [Leetcode] 45. Jump Game II
- [leetcode] 45.Jump Game II
- [leetcode] 45.Jump Game II
- leetcode 45. Jump Game II
- LeetCode 45. Jump Game II
- LeetCode 45. Jump Game II
- leetcode.45. Jump Game II
- leetcode 45. Jump Game II
- leetcode 45. Jump Game II
- [LeetCode] 45. Jump Game II
- LeetCode 45. Jump Game II
- leetcode 45.Jump Game II
- leetcode 45. Jump Game II
- 【leetcode】45. Jump Game II
- LeetCode 45. Jump Game II
- LeetCode 45. Jump Game II
- 高通平台lk下调屏小结
- Python Cookbook 2——Python技巧
- i++ 、++i 、i-- 、--i 的效率问题
- 《影响力》
- 易语言数字指令编程大全(发送信息代码数据集合)
- Leetcode 45. Jump Game II
- Spring学习(三)
- [Unity3D]Shader编程之贴图切换
- Big O and little o
- Spring框架——IOC依赖注入
- Android中窗口的创建过程
- Matlab 1.矩阵的切割再把得到的矩阵按顺序拼接排列(1维数据按规律转为2维数据方便深度学习使用)
- Redis几种数据结构常用命令整合
- Elastic Search 的分布式特点(和MongoDB对比)