[LintCode] Largest Divisible Subset

Posted Push your limit!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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 

以上是关于[LintCode] Largest Divisible Subset的主要内容,如果未能解决你的问题,请参考以下文章

[LintCode] Largest Rectangle in Histogram

Lintcode5 Kth Largest Element solution 题解

Kth Largest Element II Lintcode

179. Largest Number

Largest Submatrix(动态规划)

Largest Number