-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestDivisibleSubset.java
More file actions
60 lines (58 loc) · 1.58 KB
/
LargestDivisibleSubset.java
File metadata and controls
60 lines (58 loc) · 1.58 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
package medium;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* ClassName: LargestDivisibleSubset.java
* Author: chenyiAlone
* Create Time: 2019/7/18 23:35
* Description: No.368 Largest Divisible Subset
*
* 1. 将数组排序
* 2. f[i] > f[j] + 1 证明比原来的要长,就保存路径到 solution 中
* 3. 将 solution 中的路径显示出来
*
*
*
*
* 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:
*
* Input: [1,2,3]
* Output: [1,2] (of course, [1,3] will also be ok)
* Example 2:
*
* Input: [1,2,4,8]
* Output: [1,2,4,8]
*
*/
public class LargestDivisibleSubset {
public List<Integer> largestDivisibleSubset(int[] nums) {
int len = nums.length;
List<Integer> ret = new ArrayList<>();
if (len == 0) return ret;
Arrays.sort(nums);
int[] f = new int[len], solution = new int[len];
Arrays.fill(f, -1);
Arrays.fill(solution, -1);
int ans = 0;
for (int i = 0; i < len; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] % nums[j] == 0 && f[i] < f[j] + 1) {
f[i] = f[j] + 1;
solution[i] = j;
}
}
if (f[i] > f[ans]) ans = i;
}
for (int i = ans; i != -1; i = solution[i]) {
ret.add(nums[i]);
}
return ret;
}
}