LeetCode 368

题目描述:

Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0.

If there are multiple solutions, return any subset is fine.

Example 1:

nums: [1,2,3]

Result: [1,2] (of course, [1,3] will also be ok)

Example 2:

nums: [1,2,4,8]

Result: [1,2,4,8]

思路:动态规划

需要注意到一点,题目要求的是整除,则当nums从小到大排列之后,对于i<j,如果 nums[k]%nums[j]==0 则一定有 nums[k]%nums[i] == 0

如此,该题目只需要维护一个一维数组即可得到结果。不过只是动归的话,仅能得到subset的大小,如果要得到subset的具体值,参照Dijkstra,再维护一个数组以记录last node,最后只需要回溯一下就OK。

AC代码:

 1 public int[] sort(int [] nums){
 2         int k, min;
 3         for (int i = 0; i< nums.length; i++){
 4             min = Integer.MAX_VALUE;
 5             k = 0;
 6             for (int j = i; j< nums.length; j++){
 7                 if ( min > nums[j]){
 8                     min = nums[j];
 9                     k = j;
10                 }
11             }
12             nums[k] = nums[i];
13             nums[i] = min;
14         }
15
16         return nums;
17     }
18
19     public List<Integer> largestDivisibleSubset(int[] nums){
20         if (nums.length <= 0) return new ArrayList<Integer>();
21         nums = sort(nums);
22         int size = nums.length;
23         int [] times = new int[size];
24         int [] index = new int[size];
25         for (int i = 0; i< size; i++){
26             times[i] = 1;
27             index[i] = -1;
28         }
29         for (int i = 0; i< size; i++){
30             for (int j = 0; j< i; j++){
31                 if (nums[i]%nums[j] == 0 && times[j] + 1 > times[i]){
32                     times[i] = times[j] + 1;
33                     index[i] = j;
34                 }
35             }
36         }
37         int k = 0, max = 0;
38         for (int i = 0; i < size; i++){
39             if (max <= times[i]){
40                 k = i; max = times[i];
41             }
42         }
43         List<Integer> res = new ArrayList<Integer>();
44         while(k >= 0){
45             res.add(nums[k]);
46             k = index[k];
47         }
48         return res;
49     }

经验&教训:

第一次我不是这样想的,第一次我是想,对于集合{a, b, c, d, e},先判断集合是否满足两两整除,如果不是,则分别考虑5个只有4个元素的子集,分别寻找子集中的最大子集。如此,复杂度为2^n。

主要是没有注意到,如果把集合排序,会有什么样的特性。有序数据真是创造奇迹的存在啊

时间: 2024-08-04 13:04:23

LeetCode 368的相关文章

Leetcode 368. Largest Divisible Subset

Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0. If there are multiple solutions, return any subset is fine. Example 1: nums: [1,2,3] Re

【leetcode】368. Largest Divisible Subset

题目描述: Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0. If there are multiple solutions, return any subset is fine. 解题分析: 如果a%b==0,则a=mb,

368.[LeetCode] Largest Divisible Subset

条件:动态规划 当nums[j]%nums[i]==0时,dp[i] = max(dp[i], dp[j]+1) 为了返回数组,使用pair记录路径 class Solution { public: vector<int> largestDivisibleSubset(vector<int>& nums) { //变量和初始值的定义 vector<int> res; if(nums.size()<1) return res;//临界条件 sort(nums

LeetCode.893-特殊相等字符串组(Groups of Special-Equivalent Strings)

这是悦乐书的第344次更新,第368篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第209题(顺位题号是893). You are given an array A of strings. Two strings S and T are special-equivalent if after any number of moves, S == T. A move consists of choosing two indices i and j with i % 2

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

LeetCode OJ - Surrounded Regions

我觉得这道题和传统的用动规或者贪心等算法的题目不同.按照题目的意思,就是将被'X'围绕的'O'区域找出来,然后覆盖成'X'. 那问题就变成两个子问题: 1. 找到'O'区域,可能有多个区域,每个区域'O'都是相连的: 2. 判断'O'区域是否是被'X'包围. 我采用树的宽度遍历的方法,找到每一个'O'区域,并为每个区域设置一个value值,为0或者1,1表示是被'X'包围,0则表示不是.是否被'X'包围就是看'O'区域的边界是否是在2D数组的边界上. 下面是具体的AC代码: class Boar