Jump Game II一道优化bfs题
来源:互联网 发布:淘宝外宣兼职违法吗 编辑:程序博客网 时间:2024/06/05 14:29
题目描述:
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A = [2,3,1,1,4]
The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
Note:
You can assume that you can always reach the last index.
分析:
考虑到如果在儿子时已经到了孙子没法达到的点,那么这个孙子节点没有必要加入队列。
代码如下:
struct mypair{ int x; int y; };int jump(vector<int>& nums) {if(nums.size()==1) return 0; queue<mypair>temp; vector<int>a; a.push_back(0); mypair k; k.x=0; k.y=0; temp.push(k); while(!temp.empty()){ mypair s=temp.front(); temp.pop(); if(s.y+nums[s.y]>=nums.size()-1) return s.x+1; while(a.size()<=s.x+1) a.push_back(0); int p=nums[s.y]; while(p>0){ k.x=s.x+1; k.y=p+s.y; if(k.y<=a[s.x]) break;//扔掉没意义的节点 if(k.y>a[s.x+1]) a[s.x+1]=k.y; temp.push(k); p--; } }}
阅读全文
0 0
- Jump Game II一道优化bfs题
- Jump Game II(另类BFS)
- 【一天一道LeetCode】#45. Jump Game II
- Jump Game &Jump Game II
- Jump Game/Jump Game II
- 每日一道算法题:Jump Game
- [leetcode刷题系列]Jump Game II
- LeetCode刷题笔录 Jump Game II
- **LeetCode 45. Jump Game II 思维题
- LeetCode: Jump Game II
- LeetCode Jump Game II
- LeetCode: Jump Game II
- [Leetcode] Jump Game II
- [LeetCode] Jump Game II
- [Leetcode] Jump Game ii
- leetcode Jump Game II
- Jump Game II
- Jump Game II
- jinfo_动态调整JVM参数(无需重启)(实践)
- 【Tenka1 Programmer Contest 2017 F】ModularPowerEquation!!(欧拉定理)
- 向数据库存储读取图片方法总结
- C++虚继承与虚函数
- <C++ Primer_5th>习题_3.16
- Jump Game II一道优化bfs题
- URAL 1011|Conductors|暴力
- <C++ Primer_5th>习题_3.17
- java序列化与反序列化(2)------jdk原生序列化机制Serializable
- 事务详解
- <C++ Primer_5th>习题_3.20
- matlab基础(2)
- 汉诺塔(Hanoi)问题递归&非递归的C++实现及总结
- 十进制转十六进制的C实现