博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode "Coin Change"
阅读量:4684 次
发布时间:2019-06-09

本文共 709 字,大约阅读时间需要 2 分钟。

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
& coins, int amount) { int n = coins.size(); if (!n) return -1; vector
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]; }};

 

转载于:https://www.cnblogs.com/tonix/p/5106607.html

你可能感兴趣的文章
@property@classmethod@staticmethod
查看>>
iOS chart 图表完美解决方案 基于swift
查看>>
【转载】API入门系列之三 -那迷惑人的Windows字符和字符指针类型
查看>>
Python标准库09 当前进程信息 (os包)
查看>>
【转】Unable to load native-hadoop library for your platform(已解决)
查看>>
HDU - 2159 FATE(二维dp之01背包问题)
查看>>
多个 additional include directories 的复制方法
查看>>
Java中 VO、 PO、DO、DTO、 BO、 QO、DAO、POJO的概念
查看>>
[COGS 2421] [HZOI 2016] 简单的Treap 笛卡尔树
查看>>
PAT Basic 1032
查看>>
Java基础之类加载器
查看>>
Codeforces 961 E Tufurama
查看>>
全球第一免费开源ERP Odoo Ubuntu最佳开发环境独家首发分享
查看>>
UVA 1594 Ducci Sequence(紫书习题5-2 简单模拟题)
查看>>
进程和线程新增
查看>>
查询库中表结构SQL语句
查看>>
切换到mint了,纪念一下
查看>>
【CS231N】6、神经网络动态部分:损失函数等
查看>>
Python学习笔记(四)——编码和字符串
查看>>
使用正则表达式,取得点击次数,函数抽离
查看>>