[LeetCode]Jump Game II
来源:互联网 发布:医药行业数据清洗 编辑:程序博客网 时间:2024/04/29 10:23
class Solution {//get the max reachable nextPos at every step O(n)public:int jump(int A[], int n) {// Start typing your C/C++ solution below// DO NOT write int main() functionif(0 == n) return 0;int nowPos = 0;int nextPos = 0;int ans = 0;while (true){if(nextPos >= n-1) break;int maxPos = -1;for (int i = nowPos; i <= nextPos; ++i)maxPos = max(i+A[i], maxPos);nowPos = nextPos;nextPos = maxPos;ans++;}return ans;}};
second time
class Solution {public: int jump(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function int k = 0; int start = 0; int end = 0; while(end < n-1) { int newEnd = INT_MIN; for(int i = start; i <= end; ++i) newEnd = max(newEnd, A[i]+i); if(newEnd == end) return -1; start = end+1; end = newEnd; ++k; } return k; }};
- 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
- 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
- LeetCode | Jump Game II
- 【Leetcode】Jump Game II
- LeetCode Jump Game II
- [LeetCode] Jump Game II
- php 初学头疼问题之:include/include_once/require/require_once的区别
- 《TCP/IP协议详解》第12章 广播和多播
- [LeetCode]Jump Game
- ArcGIS Engine中的Symbols详解
- UVA 10954 - Add All
- [LeetCode]Jump Game II
- vc中内存对齐
- 乐安全内嵌广告屏蔽原理
- [LeetCode]Largest Rectangle in Histogram
- PHP实现点击a标签的href做链接时,直接保存文件(任何类型),而不是通过浏览器直接打开下载的文件
- 分布式文件系统:原理、问题与方法
- [LeetCode]Length of Last Word
- WinCE上将文件上传至FTP服务器
- 稀疏编码(Sparse Coding)的前世今生(一)