LeetCode Largest Divisible Subset(动态规划)
来源:互联网 发布:企业淘宝账户怎么注册 编辑:程序博客网 时间:2024/06/08 17:46
题意:给出一个数组,找出子集合,使得每对可以除尽
思路:将数组从小到大排序,用动态规划方法
代码如下:
public class Solution{ public int[] largestDivisibleSubset(int[] nums) { if (null == nums || 0 == nums.length) return nums; Arrays.sort(nums); int[] count = new int[nums.length]; int[] p = new int[nums.length]; int maxCnt = 0; int startIndex = 0; for (int i = nums.length - 1; i >= 0; i--) { for (int j = i; j < nums.length; j++) { if (nums[j] % nums[i] == 0 && count[i] < 1 + count[j]) { count[i] = 1 + count[j]; p[i] = j; if (count[i] > maxCnt) { maxCnt = count[i]; startIndex = i; } } } } int[] res = new int[maxCnt]; int cnt = 0; while (startIndex != p[startIndex]) { res[cnt++] = nums[startIndex]; startIndex = p[startIndex]; } res[cnt++] = nums[startIndex]; return res; }}
0 0
- LeetCode Largest Divisible Subset(动态规划)
- 动态规划之 368Largest Divisible Subset
- 动态规划-368. Largest Divisible Subset
- 动态规划解368. Largest Divisible Subset
- 【Leetcode】Largest Divisible Subset
- 【Leetcode】Largest Divisible Subset
- <LeetCode>Largest Divisible Subset
- Leetcode Largest Divisible Subset
- [LeetCode] Largest Divisible Subset
- Leetcode 368 - Largest Divisible Subset(dp)
- [LeetCode]:动态规划+回溯 Largest Divisible Subset 最大可整除的子集合
- leetcode 368. Largest Divisible Subset 类似LISS最长递增子序列问题 + DP动态规划
- [leetcode] 368. Largest Divisible Subset
- Leetcode 368. Largest Divisible Subset
- LeetCode 368. Largest Divisible Subset
- LeetCode 368 Largest Divisible Subset
- leetcode 368. Largest Divisible Subset
- [leetcode] 368. Largest Divisible Subset
- 判断字符串中是否有相同的字符
- Java 下一代: Groovy、Scala 和 Clojure 中的共同点
- Java猜拳游戏
- spark机器学习笔记:(二)用Spark Python进行数据处理和特征提取
- js正则实现前端搜索,匹配字符加红
- LeetCode Largest Divisible Subset(动态规划)
- mongodb的聚合函数的$group方法运用。
- DrawerLayout的简单使用
- 定时调度线程池scheduleAtFixedRate和schedule方法
- 经常用到的Itent完全理解
- jackson 对象转换成json
- rsync+inotify实现服务器之间文件实时同步
- 如何在 Flickr 上找到又酷,又有趣,且版权自由的照片?
- 水仙花数和递归调用举例