HackerRank "Jumping on the Clouds"
DP or Greedy - they are all in O(n)
In editorial, a beautiful Greedy solution is given: "To reach the last cloud in a minimum number of steps, always try make a jump from i to i + 2. If that is not possible, jump to i + 1. ". And here is my DP solution:
#include <vector>
#include <iostream> using namespace std; int main(){
int n;
cin >> n;
vector<int> c(n);
for(int c_i = ;c_i < n;c_i++){
cin >> c[c_i];
} vector<int> dp(n, INT_MAX);
dp[] = ;
for(int i = ; i < n; i++)
{
if(i > && !c[i-])
{
dp[i] = min(dp[i] ,dp[i - ] + );
}
if(i> && !c[i - ])
{
dp[i] = min(dp[i], dp[i - ] + );
}
}
cout << dp.back() << endl;
return ;
}
HackerRank "Jumping on the Clouds"的更多相关文章
- E - Super Jumping! Jumping! Jumping!
/* Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popula ...
- Bungee Jumping[HDU1155]
Bungee JumpingTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Super Jumping! Jumping! Jumping!
Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. May ...
- DP专题训练之HDU 1087 Super Jumping!
Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is ve ...
- 日常小测:颜色 && Hackerrank Unique_colors
题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...
- hdu 1087 Super Jumping! Jumping! Jumping! 简单的dp
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Super Jumping! Jumping! Jumping!——E
E. Super Jumping! Jumping! Jumping! Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO forma ...
- HackerRank "Square Subsequences" !!!
Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...
- HackerRank "Minimum Penalty Path"
It is about how to choose btw. BFS and DFS. My init thought was to DFS - TLE\MLE. And its editorial ...
随机推荐
- [转]PYTHON-SCRAPY-WINDOWS下的安装笔记
分类: Crawler.Net Python2014-05-15 13:36 127人阅读 评论(0) 收藏 举报 PYTHON SCRAPY 1.安装PYTHON2.7.6,下载地址:https:/ ...
- 深入理解JavaScript系列:为什么03-0.2不等于0.1
五一宅家看书,所以接着更新一篇文章. 今天讲一下为什么03-0.2不等于0.1这个问题. 有点标题党的味道,在JavaScript中,当你试着对小数进行加减运算时,有时候会发现某个结果并非我们所想的那 ...
- Singleton Design Pattern
The Singleton pattern is one of the simplest design patterns, which restricts the instantiation of a ...
- 佛祖保佑,永无bug
/* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : | ...
- Resharp注册码
admin@youbaozang.comSpFEMUSrPM0AGupqlNs6J1Ey7HrjpJZy admin@wuleba.comd6GuozPm+bsCmPOtyJ2w1ggRnCr3Vk5 ...
- IOS 宏定义一个单例
有时候是不是因为频繁地创建一个单例对象而头疼,一种方式要写好多遍?当然你可以用OC语言进行封装.但下面将介绍一种由C语言进行的封装.只要实现下面的方法,以后建单例对象只要二句话. 1.新建一个.h文件 ...
- Android 学习第16课,java 包、类等相关的一些基础知识
1.建议将类放在包中,不要使用无名包 2.建议包名都用小写单词组成,不要用大写 3.建议包名用“域名的倒写.项目名.模块名”的形式,以确保包名的唯一性 注意:类变量与实例变量.类方法与实例方法的区别 ...
- Launching the Debugger Automatically
You can set up your application to start Visual Studio when you launch the application from Windows. ...
- guess number
crossin的前面几章基本和LPTHW内容重合,因此我直接做了他前面的一个综合练习. 猜数游戏, 即系统随机记录一个数,根据用户猜的记录,如果正确则告知,且退出游戏,如不正确,则提示答案与用户输入的 ...
- thinkphp 介绍
一.ThinkPHP的介绍 MVC M - Model 模型 工作:负责数据的操作 V - View 视图(模板) 工作:负责 ...