[LintCode] 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

Given nums = [1,2,3], return [1,2] or [1,3]

Given nums = [1,2,4,8], return [1,2,4,8]

Clarification: If there is no such pair, then a single integer should be returned. For example, nums = [3, 5, 7], then 3 or 5 or 7 should be returned as the only integer in the subset.

Algorithm

1. Sort the given array in ascending order.

2. Use dynamic programming as follows to get one such largest subset.

State: count[i] is the number of integers of the subset that satisifies the divisible condition. nums[i] is the largest integer of this subset.

Function: count[i] = {max(count[j]), for all j from 0 to i - 1} + 1 if nums[i] % nums[j] == 0;  count[i] = 0, if no such j meets the divisible condition.

Initialization: count[i] = 1.

3. Fill in all values of count[].

4. Find the max value in count[] and its index idx.

5. Starting from nums[idx] and traverse nums backwards, adding all numbers that belong in the result subset.

 1 public class Solution {
 2     /**
 3      * @param nums a set of distinct positive integers
 4      * @return the largest subset
 5      */
 6     public List<Integer> largestDivisibleSubset(int[] nums) {
 7         List<Integer> result = new ArrayList<Integer>();
 8         if(nums == null || nums.length <= 1){
 9             return result;
10         }
11         int n = nums.length;
12         Arrays.sort(nums);
13         int[] count = new int[n];
14         for(int i = 0; i < n; i++){
15             count[i] = 1;
16         }
17         for(int i = 1; i < n; i++){
18             int maxNum = 0;
19             for(int j = 0; j < i; j++){
20                 if(nums[i] % nums[j] == 0){
21                     maxNum = Math.max(maxNum, count[j]);
22                 }
23             }
24             count[i] = maxNum + 1;
25         }
26         int maxNum = Integer.MIN_VALUE;
27         int idx = 0;
28         for(int i = 0; i < n; i++){
29             if(count[i] > maxNum){
30                 maxNum = count[i];
31                 idx = i;
32             }
33         }
34         int val = nums[idx];
35         result.add(val);
36         for(int i = idx - 1; i >= 0; i--){
37             if(val % nums[i] == 0){
38                 result.add(nums[i]);
39                 val = nums[i];
40             }
41         }
42         return result;
43     }
44 }

Related Problems

Longest Increasing Subsequence

时间: 2024-10-07 19:51:20

[LintCode] Largest Divisible Subset的相关文章

368. Largest Divisible Subset

/* * 368. Largest Divisible Subset * 2016-7-16 by Mingyang * 和LIS很相似,dp[i]表示nums数组从0到i的最大集合的size. * 这题应该分成两个问题:得到最大集合size,输出这个集合 * 对于第一个问题,最大集合size就是dp数组的最大值,可以边画表边维护一个当前最大值; * 对于第二个问题,我们要维护一个parent数组,记录nums[i]加入到了哪个集合; * dp[i] = max(dp[i], dp[j] + 1

【Leetcode】Largest Divisible Subset

题目链接:https://leetcode.com/problems/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

[LeetCode] 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,

Leetcode: 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

Largest Divisible Subset -- LeetCode

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. Example 1: nums: [1,2,3] Re

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

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