leetcode322 coin change & leetcode343 integer break

来源:互联网 发布:淘宝直播关注了在哪找 编辑:程序博客网 时间:2024/05/17 09:09

此次将动态规划专题里面的两个类型的题目统一做一个记录,这两道题都有一个共同的动态规划的特性,利用前面的最优解推出后面的最有解,空间复杂度为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 than 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 forend 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[n];    }};
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝卖家评价差怎么办 淘宝卖家发货未揽收怎么办 周不过5我超过了怎么办 淘宝申请退款后卖家不确认怎么办 淘宝直通车b类扣12分怎么办 淘宝店铺被买家投诉怎么办 淘宝店铺遭买家投诉怎么办 淘宝店铺被买家恶意投诉怎么办 苹果手机无法识别指纹怎么办 淘宝店铺虚假交易违规怎么办 斗鱼直播太卡怎么办 神笔添加视频尺码不符合怎么办 闲鱼卖家单号填错了怎么办 户户通没有信号强度怎么办 全民k歌qq登不上怎么办 手机直播没电了怎么办 淘宝退货卖家拒收怎么办 充的会员卡店家关门了怎么办 淘宝手机号码被注册了怎么办 淘宝不支持七天无理由退货怎么办 微信视频横屏怎么办 房子里潮气太重怎么办 淘宝不小心注销了怎么办 淘宝号不小心注销了怎么办 xp网络驱动没了怎么办 淘宝卖家客服态度差怎么办 怀孕吃辣椒喉咙好痛怎么办 淘宝店铺建议不要提交认证怎么办 淘宝买东西商家不退款怎么办 淘宝买东西商家不发货怎么办 在微信上买东西被骗了怎么办 新浪微博自动关注人怎么办 搜淘宝找不到关键词和店铺怎么办 小超市开在一起竞争太大怎么办 淘宝店铺被投诉盗图怎么办 充电宝ic坏了怎么办 淘宝店宝贝权重下降怎么办 淘宝卖家评分低怎么办 淘宝买东西客服不理人怎么办 支付宝本次交易嫌疑违规怎么办 支付宝一年的交易总额怎么办