leetcode Jump Game I II 待续 贪心看不懂啊!!!!
下面是这两个题的解法:
参考博客:http://blog.csdn.net/loverooney/article/details/38455475
自己写的第一题(TLE):
#include<iostream>
#include<vector> using namespace std; bool canJump(vector<int>& nums)
{
int L = nums.size();
bool *flag = new bool[L];
for (int i = L - ; i >= ; i--)
{
int right = i + nums[i];
if (right >= L - )
flag[i] = true;
else
{
flag[i] = false;
for (int j = i + ;j<L&& j < i + nums[i]; j++)
{
if (flag[j] == true)
{
flag[i] = true;
break;
}
}
}
}
return flag[];
} int main()
{
vector<int> a = { , , , , };
cout << canJump(a) << endl;
}
贪心用step维护当前可到达的最远位置,每次的i必须在这个最远位置以内,假如不在就表明不能前进了return false。
代码:
#include<iostream>
#include<vector> using namespace std; bool canJump(vector<int>& nums)
{
int L = nums.size();
int step = ;
for (int i = ; i <= step; i++)
{
if (nums[i] + i > step)
step = nums[i] + i;
if (step >= L - )
return true;
}
return false;
} int main()
{
int a[] = { , , , , };
cout << canJump(vector<int>(a, a+)) << endl;
}
第二题:
BFS解法(MLE):
#include<vector>
#include<iostream>
#include<queue> using namespace std; typedef struct Node
{
int index;
int jumpNo;
Node(int index, int jumpNo) :index(index), jumpNo(jumpNo){};
}Node; // BFS
int jump(vector<int>& nums)
{
int i = ;
int L = nums.size();
queue<Node> que;
que.push(Node(i,));
while (!que.empty())
{
Node now = que.front();
que.pop();
for (i = now.index + ; i <= nums[now.index] + now.index; i++)
{
if (nums[i]+now.index >= L - )
{
return now.jumpNo+;
}
else
que.push(Node(i,now.jumpNo+));
}
}
return -;
} int main()
{
vector<int> a = { , , , , };
cout << jump(a) << endl;
}
自己写的也是MLE:
#include<iostream>
#include<vector>
#include<queue> using namespace std; typedef struct Node
{
int val; //从该节点能调到哪儿
int len; //该节点在第len跳被访问
int index; //当前节点的下标
Node(int val,int index, int len) :val(val), index(index),len(len){};
Node(){};
}Node; int jump(vector<int>& nums)
{
int L = nums.size();
Node* nodeArray = new Node[L];
for (int i = ; i < L; i++)
{
nodeArray[i].val = nums[i];
nodeArray[i].index = i;
nodeArray[i].len = ;
}
//vector<bool> visited(L,false);
queue<Node> visiteQueue;
nodeArray[].len = ;
visiteQueue.push(nodeArray[]);
while (!visiteQueue.empty())
{
Node temp = visiteQueue.front();
int index = temp.index;
int canStep = temp.val;
for (int i = index + ; i <= index + canStep; i++)
{
if (nodeArray[i].index+nodeArray[i].val>=L-)
{
return temp.len + ;
}
Node nextNode(nodeArray[i].val,nodeArray[i].index, temp.len + );
visiteQueue.push(nextNode);
}
visiteQueue.pop();
}
return false;
} int main()
{
vector<int> a = { ,,, , , ,,,,};
cout << jump(a) << endl;
}
内存不超的BFS:http://www.myext.cn/c/a_4468.html
动态规划解法(TLE):
#include<iostream>
#include<vector> using namespace std; int jump(vector<int>& nums)
{
int L = nums.size();
int *dp = new int[L];
dp[] = ;
for (int i = ; i < L; i++)
{
int min = INT_MAX;
for (int j = ; j < i; j++)
{
if (nums[j] + j >= i&&dp[j]+<min)
{
min = dp[j] + ;
}
}
dp[i] = min;
}
for (int i = ; i < L; i++)
cout << dp[i] << endl;
return dp[L - ];
} int main()
{
vector<int> a = { , , , , };
cout << jump(a) << endl;
}
贪心解法:
leetcode Jump Game I II 待续 贪心看不懂啊!!!!的更多相关文章
- Jump Game I&&II——入门级贪心算法
Jump Game I Given an array of non-negative integers, you are initially positioned at the first index ...
- LeetCode:Jump Game I II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- 对于挑战书上的很久之前都看不懂的DP看懂的突破
突破一..牢记问题概念 并且牢记dp状态方程 突破二..一直有一个求和dp转化成O1dp递推的式子看不懂.. 看不懂的原因是..没有分清求和符号作用的范围 提醒:以后遇到求和符号一定明确其求和的式子的 ...
- QQ地图api里的 地址解析函数 看不懂 javascript_百度知道
QQ地图api里的 地址解析函数 看不懂 javascript_百度知道 QQ地图api里的 地址解析函数 看不懂 javascript 2011-09-18 12:18 匿名 ...
随机推荐
- BZOJ3052:[WC2013]糖果公园
浅谈莫队:https://www.cnblogs.com/AKMer/p/10374756.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...
- wpf中为DataGrid添加checkbox支持多选全选
项目中用到DataGrid, 需要在第一列添加checkbox, 可以多选.全选. 其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellContro ...
- 在ARM模式下捕获VM并创建新VM
在ASM模式下,可以通过Manage Portal上捕获VM的Image,并创建新的VM.在ARM模式下,在Portal上目前还没有这个功能,要做VM镜像的捕获和创建新的VM需要用powershell ...
- [转载]Linux C 字符串函数 sprintf()、snprintf() 详解
一.sprintf() 函数详解 在将各种类 型的数据构造成字符串时,sprintf 的强大功能很少会让你失望. 由于 sprintf 跟 printf 在用法上几乎一样,只是打印的目的地不同而已,前 ...
- STM32 -- 硬件知识
一.网站资源 1.http://www.stmcu.com.cn/ 二.硬件 1.BOOT0 和 BOOT1 1)一般BOOT0和BOOT1跳线都跳到0(地): 只是在ISP下载的情况下,BOO ...
- 机器学习:PCA(高维数据映射为低维数据 封装&调用)
一.基础理解 1) PCA 降维的基本原理 寻找另外一个坐标系,新坐标系中的坐标轴以此表示原来样本的重要程度,也就是主成分:取出前 k 个主成分,将数据映射到这 k 个坐标轴上,获得一个低维的数据集. ...
- Codeforces Round #310 (Div. 2)556ABCDE
https://github.com/Anoxxx/OI/blob/master/Anoxx/Contest10 github自取
- Oracle、SqlServer——临时表
一.oracle 1.概述: oracle数据库的临时表的特点: 临时表默认保存在TEMP中: 表结构一直存在,直到删除:即创建一次,永久使用: 不支持主外键. 可以索引临时表和在临时表基础上建立视图 ...
- apache 禁delete
<VirtualHost *:80>ServerAdmin sunqz@jerei.comDocumentRoot /web/dasdf ServerName www.abc.com &l ...
- 解决实现OnPageChangeListener()接口的方法参数出现arg0,arg1的现象
在安卓开发中,我们经常用到ViewPager,那么既然用到ViewPager,一般都需要去实现OnPageChangeListener()接口的三个方法. 但是有时候实现的三个方法的参数都变成agr0 ...