Skip to content

Commit fbf59c4

Browse files
refactor 368
1 parent b6ba2ae commit fbf59c4

File tree

2 files changed

+70
-71
lines changed

2 files changed

+70
-71
lines changed

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,38 @@
2424
*/
2525
public class _368 {
2626

27-
/**Credit: https://discuss.leetcode.com/topic/49652/classic-dp-solution-similar-to-lis-o-n-2*/
28-
public List<Integer> largestDivisibleSubset(int[] nums) {
29-
int len = nums.length;
30-
int[] count = new int[len];
31-
int[] pre = new int[len];
32-
Arrays.sort(nums);
33-
int max = 0;
34-
int index = -1;
35-
for (int i = 0; i < len; i++) {
36-
count[i] = 1;
37-
pre[i] = -1;
38-
for (int j = i - 1; j >= 0; j--) {
39-
if (nums[i] % nums[j] == 0) {
40-
if (1 + count[j] > count[i]) {
41-
count[i] = count[j] + 1;
42-
pre[i] = j;
27+
public static class Solution1 {
28+
/** Credit: https://discuss.leetcode.com/topic/49652/classic-dp-solution-similar-to-lis-o-n-2 */
29+
public List<Integer> largestDivisibleSubset(int[] nums) {
30+
int len = nums.length;
31+
int[] count = new int[len];
32+
int[] pre = new int[len];
33+
Arrays.sort(nums);
34+
int max = 0;
35+
int index = -1;
36+
for (int i = 0; i < len; i++) {
37+
count[i] = 1;
38+
pre[i] = -1;
39+
for (int j = i - 1; j >= 0; j--) {
40+
if (nums[i] % nums[j] == 0) {
41+
if (1 + count[j] > count[i]) {
42+
count[i] = count[j] + 1;
43+
pre[i] = j;
44+
}
4345
}
4446
}
47+
if (count[i] > max) {
48+
max = count[i];
49+
index = i;
50+
}
4551
}
46-
if (count[i] > max) {
47-
max = count[i];
48-
index = i;
52+
List<Integer> res = new ArrayList<>();
53+
while (index != -1) {
54+
res.add(nums[index]);
55+
index = pre[index];
4956
}
57+
return res;
5058
}
51-
List<Integer> res = new ArrayList<>();
52-
while (index != -1) {
53-
res.add(nums[index]);
54-
index = pre[index];
55-
}
56-
return res;
5759
}
5860

5961
}

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

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,48 @@
99
import static org.hamcrest.core.Is.is;
1010
import static org.junit.Assert.assertThat;
1111

12-
/**
13-
* Created by fishercoder on 5/28/17.
14-
*/
1512
public class _368Test {
16-
private static _368 test;
17-
private static int[] nums;
18-
19-
@BeforeClass
20-
public static void setup() {
21-
test = new _368();
22-
}
23-
24-
@Test
25-
public void test1() {
26-
nums = new int[]{1, 2, 4, 8};
27-
assertThat(test.largestDivisibleSubset(nums), is(Arrays.asList(8, 4, 2, 1)));
28-
}
29-
30-
@Test
31-
public void test2() {
32-
nums = new int[]{1, 2, 3};
33-
assertThat(test.largestDivisibleSubset(nums), is(Arrays.asList(2, 1)));
34-
}
35-
36-
@Test
37-
public void test3() {
38-
nums = new int[]{1};
39-
assertThat(test.largestDivisibleSubset(nums), is(Arrays.asList(1)));
40-
}
41-
42-
@Test
43-
public void test4() {
44-
nums = new int[]{546, 669};
45-
assertThat(test.largestDivisibleSubset(nums), is(Arrays.asList(546)));
46-
}
47-
48-
@Test
49-
public void test5() {
50-
nums = new int[]{};
51-
assertThat(test.largestDivisibleSubset(nums), is(Arrays.asList()));
52-
}
53-
54-
@Test
55-
public void test6() {
56-
nums = new int[]{4, 8, 10, 240};
57-
assertThat(test.largestDivisibleSubset(nums), is(Arrays.asList(240, 8, 4)));
58-
}
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+
}
5956
}

0 commit comments

Comments
 (0)