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 ...
随机推荐
- X删除数据表的新用法
删除数据表,可以这样进行,以前傻不拉唧的用sql去手动删除. DAL dal = ... dal.Db.CreateMetaData().SetSche ...
- Go http共享
package main import( "net/http" "fmt" ) func main(){ h := http.FileServer(http.D ...
- js 控制框架页面跳转 top.location.herf = "url"
top.location.href和localtion.href有什么不同 top.location.href=”url” 在顶层页面打开url(跳出框架) self.locatio ...
- maven - Eclipse构建maven项目
前面的博文已经介绍了如何安装maven,本文将记录如何在Eclipse下构建maven项目. 一.Eclipse maven插件安装 关于安装Eclipse maven插件,网上有很多方法,这里推荐一 ...
- 【css】a标签的用法
<a>标签属性display的不同设置达到目的 display:block和display:inline; display:block 可以使得<a>标签设置宽高.边线.mar ...
- Javaweb学习随笔_JSP的九大内置对象
JSP内置对象整理 1. 九大内置对象: out,request,response,session,application,page,pageContext,config,Exception. 1.1 ...
- 1.Counting DNA Nucleotides
Problem A string is simply an ordered collection of symbols selected from some alphabet and formed i ...
- apply 伴生对象 单例对象
apply(): 当类或者对象有一个主要用途时,apply方法提供了很好语法机制 scala> class Foo {} defined class Foo scala> object F ...
- SpringMVC中定时器继承Task后无法对service注入问题
最近在做一个Spring+MyBatis的一个项目,其中用到了Redis的存储,然后遇到问题是这样的: RedisTask是通过定时器来每分钟像数据库里推送的,于是就有了 public class R ...
- final关键字
1.final类不能被继承,因此final类的成员方法没有机会被覆盖,默认都是final的.在设计类时候,如果这个类不需要有子类,类的实现细节不允许改变,并且确信这个类不会再 被扩展,那么就设计为fi ...