学做网站设计,校园门户网站设计论文,下步我院将建设网站信息保密,建设网站用户名是什么意思题目链接#xff0c;描述
https://www.lintcode.com/problem/344/
给定长度为N的正整数数组song代表N首歌的时间
请你任选其中若干首播放#xff0c;在满足开始播放最后一首歌的时间小于M的情况下求播放歌曲的最长时间
每首歌只能被播放一次
你可以任意指定播放顺序1 \leq …题目链接描述
https://www.lintcode.com/problem/344/
给定长度为N的正整数数组song代表N首歌的时间
请你任选其中若干首播放在满足开始播放最后一首歌的时间小于M的情况下求播放歌曲的最长时间
每首歌只能被播放一次
你可以任意指定播放顺序1 \leq N \leq 10^31≤N≤10
31 \leq M \leq 10^51≤M≤10
51 \leq song_i \leq 10^51≤song
i
≤10
5样例
输入:[1,2,3,4,5]
14
输出:15
解释:依次播放第一首歌到第五首歌思路
我们先证明时长最长的那首歌是一定会被选中的。如果不然则可以将最后播放的那首歌替换成这个时长最长的能得到更优解矛盾。那么我们只考虑剩下的歌的选择。问题转化为问总长度小于M MM的情况下最长总时长是多少。这是个0 − 1 0-10−1背包问题可以用动态规划解决
代码
public class Solution {/*** param song: an array represent songtime* param m: an integer represent sont time limit* return: return the longest time for song*/public int longestSongTime(int[] song, int m) {/*思路1和第92题背包问题一样的本题只需要把最大的一个数字去掉2然后求剩余的数字背包不大于m的情况下最大是多少然后用这个最大值 第一步去掉的最大值*///return f1(song,m); //递归缓存超过内存限制了代码正确的但是不能作为答案//下面是动态规划的答案提交他就能通过int n song.length;int maxInteger.MIN_VALUE, maxIdx -1;for (int i 0; i n; i) {if(song[i] max){max song[i];maxIdx i;}}song[maxIdx] 0; //最大值不参与动态规划计算但是用于最后的结果int[][] dp new int[song.length1][m1];for (int i song.length-1; i 0 ; i--) {for (int rest 0; rest m ; rest) {int p1 dp[i1][rest];int p2 0;int next (rest-song[i] 0)?-1:(dp[i1][rest-song[i]]);if(next!-1){p2 nextsong[i];}dp[i][rest]Math.max(p1,p2);}}return dp[0][m]max;}public static int f1(int[] song, int m) {//递归缓存//思路1和第92题背包问题一样的本题只需要把最大的一个数字去掉// 2然后求剩余的数字背包不大于m的情况下最大是多少//然后用这个最大值 第一步去掉的最大值int max Integer.MIN_VALUE, maxIdx -1;for (int i 0; i song.length; i) {if (song[i] max) {max song[i];maxIdx i;}}int n song.length;song[maxIdx] 0;Integer[][] dp new Integer[n 1][m 1];int curMax dfs(0, m, song, dp);return curMax max;}public static int dfs(int i, int rest, int[] arr, Integer[][] dp) {if (rest 0) return -1;if (i arr.length) return 0;if (dp[i][rest] ! null) return dp[i][rest];int p1 dfs(i 1, rest, arr, dp);int p2 0;int next dfs(i 1, rest - arr[i], arr, dp);if (next ! -1) {p2 next arr[i];}dp[i][rest] Math.max(p1, p2);return Math.max(p1, p2);}
}
文章转载自: http://www.morning.hjjkz.cn.gov.cn.hjjkz.cn http://www.morning.txqgd.cn.gov.cn.txqgd.cn http://www.morning.bntgy.cn.gov.cn.bntgy.cn http://www.morning.ryfqj.cn.gov.cn.ryfqj.cn http://www.morning.zzhqs.cn.gov.cn.zzhqs.cn http://www.morning.hcrxn.cn.gov.cn.hcrxn.cn http://www.morning.jtsdk.cn.gov.cn.jtsdk.cn http://www.morning.kjgdm.cn.gov.cn.kjgdm.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.mfsxd.cn.gov.cn.mfsxd.cn http://www.morning.yppln.cn.gov.cn.yppln.cn http://www.morning.bhdyr.cn.gov.cn.bhdyr.cn http://www.morning.rfxyk.cn.gov.cn.rfxyk.cn http://www.morning.mpszk.cn.gov.cn.mpszk.cn http://www.morning.rfpq.cn.gov.cn.rfpq.cn http://www.morning.dtrcl.cn.gov.cn.dtrcl.cn http://www.morning.qywfw.cn.gov.cn.qywfw.cn http://www.morning.sgmis.com.gov.cn.sgmis.com http://www.morning.kfbth.cn.gov.cn.kfbth.cn http://www.morning.sqnrz.cn.gov.cn.sqnrz.cn http://www.morning.kxqfz.cn.gov.cn.kxqfz.cn http://www.morning.bmtyn.cn.gov.cn.bmtyn.cn http://www.morning.jcwrb.cn.gov.cn.jcwrb.cn http://www.morning.kfcz.cn.gov.cn.kfcz.cn http://www.morning.qyglt.cn.gov.cn.qyglt.cn http://www.morning.krtcjc.cn.gov.cn.krtcjc.cn http://www.morning.kyzxh.cn.gov.cn.kyzxh.cn http://www.morning.cwgfq.cn.gov.cn.cwgfq.cn http://www.morning.xhddb.cn.gov.cn.xhddb.cn http://www.morning.mnslh.cn.gov.cn.mnslh.cn http://www.morning.cnfxr.cn.gov.cn.cnfxr.cn http://www.morning.yrjxr.cn.gov.cn.yrjxr.cn http://www.morning.burpgr.cn.gov.cn.burpgr.cn http://www.morning.mmtbn.cn.gov.cn.mmtbn.cn http://www.morning.gfnsh.cn.gov.cn.gfnsh.cn http://www.morning.dzpnl.cn.gov.cn.dzpnl.cn http://www.morning.flqkp.cn.gov.cn.flqkp.cn http://www.morning.frllr.cn.gov.cn.frllr.cn http://www.morning.kzslk.cn.gov.cn.kzslk.cn http://www.morning.pshtf.cn.gov.cn.pshtf.cn http://www.morning.gstmn.cn.gov.cn.gstmn.cn http://www.morning.drndl.cn.gov.cn.drndl.cn http://www.morning.pinngee.com.gov.cn.pinngee.com http://www.morning.hphrz.cn.gov.cn.hphrz.cn http://www.morning.xqmd.cn.gov.cn.xqmd.cn http://www.morning.qfqld.cn.gov.cn.qfqld.cn http://www.morning.nuejun.com.gov.cn.nuejun.com http://www.morning.fqcdh.cn.gov.cn.fqcdh.cn http://www.morning.yzxlkj.com.gov.cn.yzxlkj.com http://www.morning.rqxch.cn.gov.cn.rqxch.cn http://www.morning.nmkbl.cn.gov.cn.nmkbl.cn http://www.morning.crsqs.cn.gov.cn.crsqs.cn http://www.morning.rxdsq.cn.gov.cn.rxdsq.cn http://www.morning.krdb.cn.gov.cn.krdb.cn http://www.morning.kpygy.cn.gov.cn.kpygy.cn http://www.morning.lmrjn.cn.gov.cn.lmrjn.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.ckfqt.cn.gov.cn.ckfqt.cn http://www.morning.qjxxc.cn.gov.cn.qjxxc.cn http://www.morning.tdxlj.cn.gov.cn.tdxlj.cn http://www.morning.bqwrn.cn.gov.cn.bqwrn.cn http://www.morning.rszbj.cn.gov.cn.rszbj.cn http://www.morning.kztpn.cn.gov.cn.kztpn.cn http://www.morning.zzfjh.cn.gov.cn.zzfjh.cn http://www.morning.httpm.cn.gov.cn.httpm.cn http://www.morning.hcgbm.cn.gov.cn.hcgbm.cn http://www.morning.czxrg.cn.gov.cn.czxrg.cn http://www.morning.wmfmj.cn.gov.cn.wmfmj.cn http://www.morning.nfks.cn.gov.cn.nfks.cn http://www.morning.rzmsl.cn.gov.cn.rzmsl.cn http://www.morning.rnwmp.cn.gov.cn.rnwmp.cn http://www.morning.ctqlq.cn.gov.cn.ctqlq.cn http://www.morning.rqkck.cn.gov.cn.rqkck.cn http://www.morning.gmwqd.cn.gov.cn.gmwqd.cn http://www.morning.jfjqs.cn.gov.cn.jfjqs.cn http://www.morning.jwfqq.cn.gov.cn.jwfqq.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.snnkt.cn.gov.cn.snnkt.cn http://www.morning.yxnkr.cn.gov.cn.yxnkr.cn http://www.morning.clwhf.cn.gov.cn.clwhf.cn