forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_368.java
80 lines (75 loc) · 2.54 KB
/
_368.java
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
73
74
75
76
77
78
79
80
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class _368 {
public static class Solution1 {
/**
* DP solution, credit: https://leetcode.com/problems/largest-divisible-subset/solution/ Solution 1
*/
public List<Integer> largestDivisibleSubset(int[] nums) {
List<List<Integer>> lists = new ArrayList<>();
Arrays.sort(nums);
int len = nums.length;
if (len == 0) {
return new ArrayList<>();
}
for (int i = 0; i < len; i++) {
lists.add(new ArrayList<>());
}
for (int i = 0; i < len; i++) {
List<Integer> maxSubset = new ArrayList<>();
for (int k = 0; k < i; k++) {
if (nums[i] % nums[k] == 0 && maxSubset.size() < lists.get(k).size()) {
maxSubset = lists.get(k);
}
}
lists.get(i).addAll(maxSubset);
lists.get(i).add(nums[i]);
}
List<Integer> ans = new ArrayList<>();
for (List<Integer> list : lists) {
if (list.size() > ans.size()) {
ans.clear();
ans.addAll(list);
}
}
return ans;
}
}
public static class Solution2 {
/**
* Credit: https://discuss.leetcode.com/topic/49652/classic-dp-solution-similar-to-lis-o-n-2
*/
public List<Integer> largestDivisibleSubset(int[] nums) {
int len = nums.length;
int[] count = new int[len];
int[] pre = new int[len];
Arrays.sort(nums);
int max = 0;
int index = -1;
for (int i = 0; i < len; i++) {
count[i] = 1;
pre[i] = -1;
for (int j = i - 1; j >= 0; j--) {
if (nums[i] % nums[j] == 0) {
if (1 + count[j] > count[i]) {
count[i] = count[j] + 1;
pre[i] = j;
}
}
}
if (count[i] > max) {
max = count[i];
index = i;
}
}
List<Integer> res = new ArrayList<>();
while (index != -1) {
res.add(nums[index]);
index = pre[index];
}
return res;
}
}
}