603
来源:互联网 发布:淘宝开店年龄有限制吗 编辑:程序博客网 时间:2024/06/16 21:16
2017.9.5
开始接触动态规划的题目,一个上午的时间,绕了不少的弯路。最后在网上看解决方法,才知道使用动态规划。
接下来几天可以多做一些动态规划的题目。
重点就是要找到状态转移方程。
public class Solution { /** * @param nums a set of distinct positive integers * @return the largest subset */ /** 学习使用动态规划 * 状态转移方程为 dp[i] = max{dp[i],dp[j]+1]} */public static LinkedList<Integer> largestDivisibleSubset(int[] nums) { // Write your code hereLinkedList<Integer> res = new LinkedList<Integer>();int length = nums.length;if(length == 0){return null;}Arrays.sort(nums);/** dp 用来记录包含num[i]的最大集合的size; * pre 用来记录最大集合的路径,也就是记录包含num[i]的最大集合的前一位数字的位置。 * */int []dp = new int[length]; int []pre = new int[length];int maxSize = 1;int maxFlag = 0;//初始化for(int i = 0; i < length; i++){dp[i] = 1;pre[i] = -1;}for(int j = 1; j < length; j++){for(int i = 0; i < j; i++){if(nums[j] % nums[i] == 0 && dp[j] < dp[i] + 1){dp[j] = dp[i] + 1;pre[j] = i;if(dp[j] > maxSize){maxSize = dp[j];maxFlag = j;}}}}res.add(nums[maxFlag]);while(pre[maxFlag] != -1){res.add(nums[pre[maxFlag]]);maxFlag = pre[maxFlag];}return res; }}
阅读全文
0 0
- 603
- 603
- SRM 603
- SRM 603
- ora-603 ora-27504
- TopCoder 603 div1 & div2
- codeforces 603B (数学)
- 603B - Moodular Arithmetic
- CF #603K Indivisibility
- SRM 603 D2 L3:GraphWalkWithProbabilities
- codefoces 603A (数学 水~)
- CodeForces 603A_Alternative Thinking (DP)
- CodeForces 603A Alternative Thinking
- CF 603A. Alternative Thinking
- problem --CF 603K ----------Indivisibility
- lintcode(603)Largest Divisible Subset
- CnWizards v0.9.8.603 Build 2011.06.06
- Codeforces 603C 博弈论 + 关于博弈论理解
- vue环境搭建与创建第一个vuejs文件
- Web页面实时刷新技术探讨
- svn merge和branch
- Python JSON
- 【多校训练】hdu 6052 To my boyfriend 计数
- 603
- 管理表空间和数据文件
- python中读取大文件
- 谷歌浏览器开发调试工具中Sources面板 js调试等 完全介绍
- Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017)
- 如何编写更好的SQL查询:终极指南-第二部分
- C#时间戳和格式化日期互转
- bs4爬虫:获取百度贴吧的内容
- java web学习总结46:Filter(过滤器)常见应用