canJump&&jumpII
来源:互联网 发布:mmd一骑当千动作数据 编辑:程序博客网 时间:2024/05/15 11:20
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool canJump(int a[], int n)
{
int reach = 1;
for (int i = 0;i < reach&&reach < n;++i)
{
reach = max(reach, i + 1 + a[i]);
}
return reach >= n;
}
bool canJump1(int a[], int n)
{
if (n == 0)
return true;
int left_most = n - 1;
for (int i = n - 2;i >= 0;--i)
if (i + a[i] >= left_most)
left_most = i;
return left_most == 0;
}
bool canJump2(int a[], int n)
{
vector<int>f(n, 0);
f[0] = 0;
for (int i = 1;i < n;++i)
{
f[i] = max(f[i - 1], a[i - 1])-1;
if (f[i] < 0)
return false;
}
return f[n - 1] >= 0;
}
int jump(int A[], int n)
{
int left = 0;
int right = 0;
int step = 0;
if (n == 1)
return 0;
while (left <= right)
{
++step;
int old_right = right;
for (int i = left;i <=old_right;++i)
{
if (i + A[i] >= n - 1)
return step;
if (i + A[i]>right)
right = i + A[i];
}
left = old_right + 1;
}
}
int main()
{
int a[] = { 2,3,1,1,4 };
int n = sizeof(a) / sizeof(int);
int b[] = { 3,2,1,0,4 };
int m = sizeof(b) / sizeof(int);
cout << canJump2(a, n) << endl;
cout << canJump2(b, m) << endl;
cout << jump(a, n) << endl;
}
#include<algorithm>
#include<vector>
using namespace std;
bool canJump(int a[], int n)
{
int reach = 1;
for (int i = 0;i < reach&&reach < n;++i)
{
reach = max(reach, i + 1 + a[i]);
}
return reach >= n;
}
bool canJump1(int a[], int n)
{
if (n == 0)
return true;
int left_most = n - 1;
for (int i = n - 2;i >= 0;--i)
if (i + a[i] >= left_most)
left_most = i;
return left_most == 0;
}
bool canJump2(int a[], int n)
{
vector<int>f(n, 0);
f[0] = 0;
for (int i = 1;i < n;++i)
{
f[i] = max(f[i - 1], a[i - 1])-1;
if (f[i] < 0)
return false;
}
return f[n - 1] >= 0;
}
int jump(int A[], int n)
{
int left = 0;
int right = 0;
int step = 0;
if (n == 1)
return 0;
while (left <= right)
{
++step;
int old_right = right;
for (int i = left;i <=old_right;++i)
{
if (i + A[i] >= n - 1)
return step;
if (i + A[i]>right)
right = i + A[i];
}
left = old_right + 1;
}
}
int main()
{
int a[] = { 2,3,1,1,4 };
int n = sizeof(a) / sizeof(int);
int b[] = { 3,2,1,0,4 };
int m = sizeof(b) / sizeof(int);
cout << canJump2(a, n) << endl;
cout << canJump2(b, m) << endl;
cout << jump(a, n) << endl;
}
0 0
- canJump&&jumpII
- Spring的事务传播属性
- Android 性能调优的技术点
- LeetCodeOJ_223_Rectangle Area
- jQuery.extend 函数详解
- 架构设计最佳实践与必备素养
- canJump&&jumpII
- SharePoint 2013 IT Professional--Permission Inheritance
- AnologClock和DigitalClock控件
- FFMPEG之TimeBase成员理解
- FI与SD的集成
- 如何快速截取某段时间内的日志
- cocos js 踩坑旅程 之ccui.Button 那点事
- JasperReport学习笔记7-导出成其它格式(pdf,xls,html)
- 移动平台对 meta 标签的定义