您的位置:首页 > 其它

leetcode322 coin change & leetcode343 integer break

2017-09-24 17:33 375 查看
此次将动态规划专题里面的两个类型的题目统一做一个记录,这两道题都有一个共同的动态规划的特性,利用前面的最优解推出后面的最有解,空间复杂度为O(n),即都要运用到整个数组的数字。

先是leetcode322 coin change

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

Example 1:

coins = [1, 2, 5], amount = 11

return 3 (11 = 5 + 5 + 1)

Example 2:

coins = [2], amount = 3

return -1.

Note:

You may assume that you have an infinite number of each kind of coin.

这道题是一道动态规划非常经典的题目,即是要利用现有的纸币组合相加找出能够组成目标数组的最小纸币,但是这道题要注意的是,可能无法组成所给的要求数组,则返回-1。

我们先讨论coin为[1,2,5]的情况,dp[i]表示组成每个数目的钱的最小纸币数,如果总数为0,则组成0的纸币数量为0.如果总数为1,那么如果利用前一个总数为0的结果,得到的公式就为:1=dp[0]+1,很明显为1张。如果是2的话,就有2=min(dp[0]+1(表示数额为2的纸币),dp[1]+1(表示数额为1的纸币))=min(1,2)=1.

则3=min(dp[1]+1(表示数额为2的纸币),dp[2]+1(表示数额为1的纸币))=2。那么递推公式显然就得到:

//数额大于4,钱的面额为[1,2,5],其他情况加个循环就行,这个为了展示递推公式
dp[i]=min(dp[i-1]+1,dp[i-2]+1,dp[i-5]+1)


当然要注意到无法组成amount的情况,即我们在每次状态转移的时候,如果找不到i的组成,并且i不为0的情况,将dp[i]=-1;当判断到dp[i]=-1时,dpi+coins[k]=-1,这个具体看代码更清楚,代码如下:

class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
if(amount==0) return 0;
vector<int> dp(amount+1,0);
int MinNumber;
int csize=coins.size();
sort(coins.begin(),coins.end());
for(int i=1;i<=amount;++i)
{
MinNumber=INT_MAX;
for(int j=0;j<csize;++j)
{
if(coins[j]>i) break;
//无法组成的数字
if(dp[i-coins[j]]==-1 && (i-coins[j])!=0) continue;
MinNumber=min(MinNumber,dp[i-coins[j]]+1);
}
if(MinNumber==INT_MAX) dp[i]=-1;
else dp[i]=MinNumber;
}
return dp[amount];
}
};


还有一个类似的题目为:

343 integer break

Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.

For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).

Note: You may assume that n is not less t
ac08
han 2 and not larger than 58.

这道题要求我们对一个非负整数进行分解,并要求分解出来的数字相乘得到最大,有了上面的coin changes。这道题的思路就变得相对简单了,就是找到组成每一个数的integer break的最大值,递推公式为,dp[i]表示组成i的乘积最大值,就有dp[2]=1,dp[3]=3(dp[3]是一个容易出错的地方,下面会提出)

for i=1:n
for j=i:2
dp[i]=min(dp[i],dp[j]*(i-j));
end for
end for


但是这里要注意dp[3]的情况,因为题目要求一个数字至少要有两个数字组成,所以,正确的dp[3]=2.但是我们在计算的过程中,要以dp[3]=3计算,为什么dp[2],dp[3]会出现这种特殊情况呢,请看下面的有关评论的一句话:

If an optimal product contains a factor f >= 4, then you can replace it with factors 2 and f-2 without losing optimality, as 2*(f-2) = 2f-4 >= f. So you never need a factor greater than or equal to 4, meaning you only need factors 1, 2 and 3 (and 1 is of course wasteful and you’d only use it for n=2 and n=3, where it’s needed).

For the rest I agree, 3*3 is simply better than 2*2*2, so you’d never use 2 more than twice.

简单来说就是,当f>=4的时候,进行2和f-2的拆分,即得到2*(f-2)>=f,大于4之后进行拆分的乘积会更大,更有用。所以,1,2,3不需要进行拆分的值最大。

这道题的代码如下:

class Solution {
public:
int integerBreak(int n) {
if(n==3) return 2;
vector<int> res(n+1,0);
res[2]=1;
int maxnumber=INT_MIN;
for(int i=3;i<=n;++i)
{
for(int j=n-1;j>=2;--j)
{
maxnumber=max(maxnumber,res[j]*(i-j));
}
maxnumber=max(maxnumber,i);
res[i]=maxnumber;
}
return res
;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: