LeetCode "Coin Change"
2016-01-06 18:29
375 查看
1AC. Intuitive DP. But please note the if condition, there's a trick - we cannot build upon an invalid dp slot.
class Solution { public: int coinChange(vector<int>& coins, int amount) { int n = coins.size(); if (!n) return -1; vector<int> dp(amount + 1, INT_MAX); dp[0] = 0; for (int i = 1; i <= amount; i ++) for (auto c: coins) { int prev = i - c; if (i >= c && (prev?dp[prev]!=INT_MAX : true)) { dp[i] = min(dp[i], dp[prev] + 1); } } return (dp[amount] == INT_MAX) ? -1 : dp[amount]; } };
相关文章推荐
- 动态sql
- 插件VectorLine
- 如何从网页中调用本地exe应用程序(兼容各个浏览器)
- orcl在linux环境中创建表空间的方法
- android 读取raw下的视频文件
- 微信开发关于外部超链接的解决办法!
- 别使用嵌套事务
- ofbiz的.groovy中创建非静态类的对象
- pywinauto 自动化框架调研
- java 国际化
- android Imageview bitmap 占用内存空间的计算
- 互联网协议精简讲解
- idea快捷键
- UIVisualEffectView
- 2438: [中山市选2011]杀人游戏 tarjan+概率与期望
- mac上把代码上传至gitHub
- c#日期操作
- CHImageGuard PHP脚本代码的分享
- arc 环境下的dealloc方法
- wordpress主题模板教程