-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLDSDemo.java
More file actions
72 lines (64 loc) · 2.37 KB
/
Copy pathLDSDemo.java
File metadata and controls
72 lines (64 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
@desc 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.
@author Cradle Lee
*/
import java.util.*;
class LargestDivisibleSubset {
public List<Integer> largestDivisibleSubset(int[] nums) {
//situation of there is only one element in nums;
if(nums.length==1){
List<Integer> ans = new LinkedList<Integer>();
ans.add(nums[0]);
return ans;
}
Arrays.sort(nums);
//use dynamic programming to
TreeMap<Integer,Set<Integer>> tm = new TreeMap<Integer,Set<Integer>>();
tm.put(-1, new TreeSet<Integer>());
//System.out.println(tm);
for(int x:nums){
int temp = -1;
for(int i=0;nums[i]<x;i++){//use the numbers in nums, AkA the key in TreeMap to search
if(x%nums[i]==0)//compare the size of the set, return the key
temp = tm.get(nums[i]).size()>=tm.get(temp).size()?nums[i]:temp;
//get the key whose set is the biggest set, and key is small than x and can divide x
}
//System.out.println("put x"+x+temp+tm.get(temp));
//System.out.println(tm);
//System.out.println("==========");
//tm.put(x, tm.get(temp));
//System.out.println(tm);
//System.out.println("***********x="+x);
//tm.get(x).add(x);//great caution here.*******************************************
Set<Integer> out = new TreeSet<Integer>(tm.get(temp));
out.add(x);
tm.put(x, out);
//System.out.println(out);
}
//System.out.println(tm);
//search the set with the biggest size();
Set<Integer> bigSet = new TreeSet<Integer>();
for(int x:nums){
bigSet = tm.get(x).size()>bigSet.size()?tm.get(x):bigSet;
}
return new ArrayList<Integer>(bigSet);
}
}
class LDSDemo{
public static void main(String[] args){
LargestDivisibleSubset lsd = new LargestDivisibleSubset();
int[] nums = {1,2,3,4,8,9};
List<Integer> ans = lsd.largestDivisibleSubset(nums);
System.out.println(ans);
}
/*public static void mapTest(){
TreeMap<Integer,Set<Integer>> tm1 = new TreeMap<Integer,Set<Integer>>();
Set<Integer> set1 = new TreeSet<Integer>();
set1.add(1)
tm1.put();
}
*/
}