Jump Game

Jump Game

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.

Determine if you are able to reach the last index.

Input

First line contains a single integer N(1<=N<=100 0000), which is the length of array. Second line contains N integers, splited by spaces.

Output

If the last index is reacheable, print string “yes\n”, else print “no\n”.

Example

Input

5

2 3 1 1 4

output

yes

Explanation

Jump 1 step from index 0 to 1, then 3 steps to the last index.

题意

有一个非负整数的数组,每一个数字表示从当前数字的位置,可以往后跳的最大的步数,问能否跳到最后?

代码

1、使用vector

#include<iostream>
#include<vector>
using namespace std;

vector<int> v;
bool canJump(vector<int>& nums) {
        int n = nums.size();
		int reach = 0;
        for (int i = 0; i < n; ++i) {
            if (i > reach || reach >= n - 1) break;
            reach = max(reach, i + nums[i]);
        }
        return reach >= n - 1;
    }

int main()
{
	int n;
	cin>>n;
	int temp;
	for(int i=0;i<n;i++){
		cin>>temp;
		v.push_back(temp);
	}
	if(canJump(v)){
		cout<<"Yes"<<endl;
	}
	else{
		cout<<"No"<<endl;
	}
	return 0;
}

2、使用数组
reach表示当前这个位置能够到达的最远的位置,在i<=reach,也就是能够跳到当前位置的情况下,遍历数组中的每个数,如果reach<i+num[i],更新cur,直到:
reach>=n-1时,表示能够跳到终点,返回true
i>reach时,不能达到终点,跳出循环,返回false

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

int a[1000010];
bool jumpGame(int num[],int N){
    //    写成int N=sizeof(num)/sizeof(num[0]);好像也对,但是查了资料说好像数组放在参数的位置表示的第一个元素位置的指针,所以sizeof(num)=4,也就是一个int的大小,这里避免错误,就加了一个参数n
    //strlen()是用在字符串里面的    
		int reach=0;
        for(int i=0;i<=reach;i++){
            if(reach<i+num[i]) reach=i+num[i];
            if(reach>=N-1) return true;
        }
        return false;
    }
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	if(jumpGame(a,n)){
		cout<<"Yes"<<endl;
	}	
	else{
		cout<<"No"<<endl;
	}
	return 0;
}

~~
~~

Jump Game ||

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.

Input

First line contains a single integer N(1<=N<=100 0000), which is the length of array. Second line contains N integers, splited by spaces.

Output

If the last index is reacheable, print the minimum number of jumps to reach the last index, else print -1.

Example

Input

5

2 3 1 1 4

output

2

Explanation

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.

题意

背景与上一题相同,但是要求如果能够跳到终点,则输出最少的步数,如果不能跳到终点,则输出-1

思路

  • 贪心算法,因为给出的是每一个点所能跳的最多的步数,从第一个点开始,我们可以得到它的跳越范围,也就是第一步的跳越范围,那么遍历这个点的跳越范围内的点,每个点又有一个跳越范围,通过遍历过程中不断地更新跳越范围,我们可以得到第二步的最远跳越范围······直到这个跳越范围达到了终点,我们就可以知道这是在第几步的范围,在第几步能够到达。
  • 注意这里不是最大化每一个点的跳越范围,因为即使选择当前最大跳越范围的点,仍不能保证最后的结果是最优的,这里是最大化每一步的跳越范围。
  • 用res记录步数,用pre记录上一步的最大跳越范围,cur记录这一步的最大跳越范围,每次循环开始的时候,先将上一步的cur赋值给pre,然后再在这个pre的范围内遍历更新这一步的cur

代码

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> v;
int jump(vector<int> &nums)
{
	int res=0,i=0,pre,cur=0;
	int n=nums.size();
	while(cur<n-1)
	{
		res++;
		pre=cur;
		for(;i<=pre;i++){
			cur=max(cur,nums[i]+i);
		}
		if(cur==pre) return -1;
		
	}
	return res;
}

int main()
{
	int n;
	cin>>n;
	int temp;
	for(int i=0;i<n;i++){
		cin>>temp;
		v.push_back(temp);
	}
	int result=jump(v);
	cout<<result<<endl;
	return 0; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值