LeetCode 45
Jump Game II
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.)
/*************************************************************************
> File Name: LeetCode045.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Tue 13 May 2016 14:47:33 PM CST
************************************************************************/ /************************************************************************* Jump Game II 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.) ************************************************************************/ #include <stdio.h> int jump(int* nums, int numsSize)
{
if( numsSize <= )
{
return ;
} int left = nums[];
int right = nums[];
int count = ;
int max = ; int i;
for( i=; i<numsSize; i++ )
{
if( i > left )
{
left = right;
count ++;
}
right = (i+nums[i])>right ? (i+nums[i]) : right;
if( left >= numsSize- )
{
return count;
}
}
return ;
} int main()
{
int nums[] = {,,,,};
int numsSize = ; int ret = jump( nums, numsSize );
printf("%d\n", ret); return ;
}
LeetCode 45的更多相关文章
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- [LeetCode] 45. Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 45 Jump Game II(按照数组进行移动)
题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 45. 跳跃游戏 II | Python
45. 跳跃游戏 II 题目来源:https://leetcode-cn.com/problems/jump-game-ii 题目 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素 ...
- Java实现 LeetCode 45 跳跃游戏 II(二)
45. 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- 力扣Leetcode 45. 跳跃游戏 II - 贪心思想
这题是 55.跳跃游戏的升级版 力扣Leetcode 55. 跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃 ...
随机推荐
- 转】机器学习开源框架Mahout配置与入门研究
原博文出自于:http://www.ha97.com/5803.html 感谢! PS:机器学习这两年特别火,ATB使劲开百万到几百万年薪招美国牛校的机器学习方向博士,作为一个技术控,也得折腾下 ...
- F5 刷新功能
//1.引入单元 uses ShlObj; //2.执行命令 procedure TForm1.Button1Click(Sender: TObject); begin SHChangeNotify( ...
- hdu 3038 How Many Answers Are Wrong
http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 MS ( ...
- Windows Server 2003 R2 64位简体中文版下载
32位版 CD1: SHA1值:d0dd2782e9387328ebfa45d8804b6850acabf520 ed2k://|file|cn_win_srv_2003_r2_enterprise_ ...
- CodeForces 589I Lottery (暴力,水题)
题意:给定 n 和 k,然后是 n 个数,表示1-k的一个值,问你修改最少的数,使得所有的1-k的数目都等于n/k. 析:水题,只要用每个数减去n/k,然后取模,加起来除以2,就ok了. 代码如下: ...
- 在VB中使用Linq To SQLite注意事项
昨天使Linq To SQLite 支持VB,今天在VB中写了几条Linq语句,发现了几个问题: 1.在Linq To SQLite中的Linq语句查询后并不是得到的匿名数据类,而是将Linq转换为S ...
- ecshop收货人信息中修改手机号为必填
Ecshop 修改收货人信息 把电话改成选择填写 手机改为必填 (加强版) 1. 编辑根目录/js/utils.js 增加手机号码的正则表达式 参照Utils.isTel = function ( ...
- contiki Makefile.include 四个关注点<contiki学习之二>
Contiki Makefile.include 笔记 约定: makefile 包括Makefile.Makefile.xxx,并不单指Makefile 不对makefile的语法进行分析,仅仅关 ...
- WCF 新手教程二
基本知识: [ServiceContract] Attribute 能够有以下Property 的: CallbackContract 设置callback的类型:Duplicate指Service ...
- JavaScrip基础讲座 - 神奇的ProtoType
1. 什么是 prototype prototype 对于 JavaScript 的 意义重大,prototype 不仅仅是一种管理对象继承的机制,更是一种出色的设计思想 在现实生活中,我们常常说, ...