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 匿名 ...
随机推荐
- webpack入门(四)——webpack loader 和plugin
什么是loader loaders是你用在app源码上的转换元件.他们是用node.js运行的,把源文件作为参数,返回新的资源的函数. 例如,你可以用loaders告诉webpack加载 coffee ...
- Unity3D的SystemInfo类,用于获取运行设备硬件信息(CPU、显卡、类型等)
SystemInfo类中的静态变量: 中文显示: Rendering.CopyTextureSupport copyTextureSupport:(只读)支持多种复制纹理功能的情况. string ...
- Verilog数组表示及初始化
(转)Verilog数组表示及初始化 这里的内存模型指的是内存的行为模型.Verilog中提供了两维数组来帮助我们建立内存的行为模型.具体来说,就是可以将内存宣称为一个reg类型的数组,这个数组中的任 ...
- Poj 1631 Bridging signals(二分+DP 解 LIS)
题意:题目很难懂,题意很简单,求最长递增子序列LIS. 分析:本题的最大数据40000,多个case.用基础的O(N^2)动态规划求解是超时,采用O(n*log2n)的二分查找加速的改进型DP后AC了 ...
- htmlunit 自动化提交/获取网页数据,自动化测试
开源组件: https://sourceforge.net/projects/htmlunit/ demo public void post() { try { WebClient client = ...
- JDK7和JDK8新特性
参考:http://www.cnblogs.com/langtianya/p/3757993.html JDK 1.7 新特性 1,switch中可以使用字串了 String s = "te ...
- (转)在Windows平台上安装Node.js及NPM模块管理
本文转载自:http://www.cnblogs.com/seanlv/archive/2011/11/22/2258716.html 之前9月份的时候我写了一篇关于如何在Windows平台上手工管理 ...
- “百度杯”CTF比赛 2017 二月场(Misc Web)
爆破-1: 打开链接,是502 我直接在后面加个变量传参数:?a=1 出了一段代码 var_dump()函数中,用了$$a,可能用了超全局变量GLOBALS 给hello参数传个GLOBALS 得到f ...
- Android 4学习(6):概述 - 深入了解Android Activity
参考:<Professional Android 4 Application Development> 深入了解Android Activity 每一个Android Activity都对 ...
- LAMP 2.6 Apache 禁止指定user_agent
user_agent 我把它叫做浏览器标识, 目前主流的浏览器有 IE. chrome. Firefox. 360. iphone上的 Safari.Android 手机上的.百度搜索引擎.googl ...