HackerRank "Jumping on the Clouds"
2016-07-21 09:31
489 查看
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:
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 = 0;c_i < n;c_i++){ cin >> c[c_i]; } vector<int> dp(n, INT_MAX); dp[0] = 0; for(int i = 1; i < n; i++) { if(i > 0 && !c[i-1]) { dp[i] = min(dp[i] ,dp[i - 1] + 1); } if(i>1 && !c[i - 2]) { dp[i] = min(dp[i], dp[i - 2] + 1); } } cout << dp.back() << endl; return 0; }
相关文章推荐
- centos 7 下进入单用户模式修改root密码
- GUID与MBR
- xUtils 中的BitmapUtils 一些注释
- css loading
- Codeforces 689D Friends and Subsequences
- ubuntu下ssh远程登录
- hdu5726 GCD (线段树+区间gcd)
- [已开源/文章教程]独立开发 一个社交 APP 的源码/架构分享 (已上架)
- SSE指令集入门
- Ubuntu14.04卸载内核
- Hadoop--07--MapReduce高级编程
- 自定义View之组合控件
- 在做性能测试之前需要知道什么
- Hadoop2.x解决Namenode 单点故障问题
- 分析Python处理基本数据<三>
- 转:NLog 自定义日志内容,写日志到数据库;修改Nlog.config不起作用的原因
- Linux Eclipse代码提示功能设置(Java & C/C++)
- Codeforces Round #274 (Div. 1) C. Riding in a Lift
- 58-最小步数
- java进程、线程、多线程以及线程安全问题