Skip to content

Commit 91f7d57

Browse files
add a solution for 368
1 parent b231cfb commit 91f7d57

File tree

2 files changed

+84
-45
lines changed

2 files changed

+84
-45
lines changed

src/main/java/com/fishercoder/solutions/_368.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,41 @@
77
public class _368 {
88

99
public static class Solution1 {
10+
/**
11+
* DP solution, credit: https://leetcode.com/problems/largest-divisible-subset/solution/ Solution 1
12+
*/
13+
public List<Integer> largestDivisibleSubset(int[] nums) {
14+
List<List<Integer>> lists = new ArrayList<>();
15+
Arrays.sort(nums);
16+
int len = nums.length;
17+
if (len == 0) {
18+
return new ArrayList<>();
19+
}
20+
for (int i = 0; i < len; i++) {
21+
lists.add(new ArrayList<>());
22+
}
23+
for (int i = 0; i < len; i++) {
24+
List<Integer> maxSubset = new ArrayList<>();
25+
for (int k = 0; k < i; k++) {
26+
if (nums[i] % nums[k] == 0 && maxSubset.size() < lists.get(k).size()) {
27+
maxSubset = lists.get(k);
28+
}
29+
}
30+
lists.get(i).addAll(maxSubset);
31+
lists.get(i).add(nums[i]);
32+
}
33+
List<Integer> ans = new ArrayList<>();
34+
for (List<Integer> list : lists) {
35+
if (list.size() > ans.size()) {
36+
ans.clear();
37+
ans.addAll(list);
38+
}
39+
}
40+
return ans;
41+
}
42+
}
43+
44+
public static class Solution2 {
1045
/**
1146
* Credit: https://discuss.leetcode.com/topic/49652/classic-dp-solution-similar-to-lis-o-n-2
1247
*/

src/test/java/com/fishercoder/_368Test.java

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,55 @@
66

77
import java.util.Arrays;
88

9-
import static org.hamcrest.core.Is.is;
10-
import static org.junit.Assert.assertThat;
9+
import static org.junit.Assert.assertEquals;
1110

1211
public class _368Test {
13-
private static _368.Solution1 solution1;
14-
private static int[] nums;
15-
16-
@BeforeClass
17-
public static void setup() {
18-
solution1 = new _368.Solution1();
19-
}
20-
21-
@Test
22-
public void test1() {
23-
nums = new int[] {1, 2, 4, 8};
24-
assertThat(solution1.largestDivisibleSubset(nums), is(Arrays.asList(8, 4, 2, 1)));
25-
}
26-
27-
@Test
28-
public void test2() {
29-
nums = new int[] {1, 2, 3};
30-
assertThat(solution1.largestDivisibleSubset(nums), is(Arrays.asList(2, 1)));
31-
}
32-
33-
@Test
34-
public void test3() {
35-
nums = new int[] {1};
36-
assertThat(solution1.largestDivisibleSubset(nums), is(Arrays.asList(1)));
37-
}
38-
39-
@Test
40-
public void test4() {
41-
nums = new int[] {546, 669};
42-
assertThat(solution1.largestDivisibleSubset(nums), is(Arrays.asList(546)));
43-
}
44-
45-
@Test
46-
public void test5() {
47-
nums = new int[] {};
48-
assertThat(solution1.largestDivisibleSubset(nums), is(Arrays.asList()));
49-
}
50-
51-
@Test
52-
public void test6() {
53-
nums = new int[] {4, 8, 10, 240};
54-
assertThat(solution1.largestDivisibleSubset(nums), is(Arrays.asList(240, 8, 4)));
55-
}
12+
private static _368.Solution1 solution1;
13+
private static _368.Solution2 solution2;
14+
private static int[] nums;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _368.Solution1();
19+
solution2 = new _368.Solution2();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
nums = new int[]{1, 2, 4, 8};
25+
assertEquals(Arrays.asList(1, 2, 4, 8), solution1.largestDivisibleSubset(nums));
26+
assertEquals(Arrays.asList(8, 4, 2, 1), solution2.largestDivisibleSubset(nums));
27+
}
28+
29+
@Test
30+
public void test2() {
31+
nums = new int[]{1, 2, 3};
32+
assertEquals(solution1.largestDivisibleSubset(nums), Arrays.asList(1, 2));
33+
assertEquals(solution2.largestDivisibleSubset(nums), Arrays.asList(2, 1));
34+
}
35+
36+
@Test
37+
public void test3() {
38+
nums = new int[]{1};
39+
assertEquals(solution1.largestDivisibleSubset(nums), Arrays.asList(1));
40+
}
41+
42+
@Test
43+
public void test4() {
44+
nums = new int[]{546, 669};
45+
assertEquals(solution1.largestDivisibleSubset(nums), Arrays.asList(546));
46+
}
47+
48+
@Test
49+
public void test5() {
50+
nums = new int[]{};
51+
assertEquals(solution1.largestDivisibleSubset(nums), Arrays.asList());
52+
}
53+
54+
@Test
55+
public void test6() {
56+
nums = new int[]{4, 8, 10, 240};
57+
assertEquals(solution1.largestDivisibleSubset(nums), Arrays.asList(4, 8, 240));
58+
assertEquals(solution2.largestDivisibleSubset(nums), Arrays.asList(240, 8, 4));
59+
}
5660
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy