LeetCode(45) Jump Game II
来源:互联网 发布:詹姆斯数据预测 编辑:程序博客网 时间:2024/05/19 19:58
题目:
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.)
用贪心策略,选择跳到下一步能跳到最远的距离。复杂度是最小步数乘以n,最坏是O(n²)。
代码:
还有O(n)的解法:记录目前的步数、上一个步数能到的最远距离、这个步数能到的最远距离。从左到右扫描数组,如果上一步不能到达某个点,步数加1。具体看代码。交了发现比解法一还慢。
代码:
阅读全文
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
- 深度理解“指令集”
- python zipfile模块
- LAMP环境 服务器多项目,多域名配置
- ANT 构建和部署项目
- SpringBoot:spring boot集成mybatis
- LeetCode(45) Jump Game II
- 关于服务器的负载均衡问题
- Spring 框架(一)
- Spring 框架(二)
- Node.js 操作Mongodb
- nodejs项目windows下开机自启动
- Spring 框架(三)
- 170728 逆向-RouterScan(4)
- Spring Boot中MyBatis的使用