Skip to content

Commit a1d999e

Browse files
update 740
1 parent 2801968 commit a1d999e

File tree

2 files changed

+58
-19
lines changed

2 files changed

+58
-19
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_740.java

+31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
35
import java.util.TreeMap;
46

57
public class _740 {
@@ -59,4 +61,33 @@ public int deleteAndEarn(int[] nums) {
5961
return curr;
6062
}
6163
}
64+
65+
public static class Solution3 {
66+
//use DP, this is basically the same code as https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/_3186.java
67+
//except here it's current - 1, in the above it's current - 2
68+
public int deleteAndEarn(int[] nums) {
69+
TreeMap<Integer, Integer> treeMap = new TreeMap<>();
70+
for (int num : nums) {
71+
treeMap.put(num, treeMap.getOrDefault(num, 0) + 1);
72+
}
73+
List<Integer> sortedList = new ArrayList<>(treeMap.keySet());
74+
int[] dp = new int[sortedList.size()];
75+
dp[0] = sortedList.get(0) * treeMap.get(sortedList.get(0));
76+
for (int i = 1; i < sortedList.size(); i++) {
77+
int current = sortedList.get(i);
78+
int currentTotal = current * treeMap.get(current);
79+
int j = i - 1;
80+
//we keep going to the left of the sorted list until we find a value that's not in the range of current - 1 if possible
81+
while (j >= 0 && sortedList.get(j) >= current - 1) {
82+
j--;
83+
}
84+
if (j >= 0) {
85+
dp[i] = Math.max(dp[i - 1], currentTotal + dp[j]);
86+
} else {
87+
dp[i] = Math.max(dp[i - 1], currentTotal);
88+
}
89+
}
90+
return dp[dp.length - 1];
91+
}
92+
}
6293
}

Diff for: src/test/java/com/fishercoder/_740Test.java

+27-19
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._740;
4-
import org.junit.BeforeClass;
5-
import org.junit.Test;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
66

7-
import static org.junit.Assert.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
88

99
public class _740Test {
10-
private static _740.Solution1 solution1;
11-
private static int[] nums;
10+
private static _740.Solution1 solution1;
11+
private static _740.Solution2 solution2;
12+
private static _740.Solution3 solution3;
13+
private static int[] nums;
1214

13-
@BeforeClass
14-
public static void setup() {
15-
solution1 = new _740.Solution1();
16-
}
15+
@BeforeEach
16+
public void setup() {
17+
solution1 = new _740.Solution1();
18+
solution2 = new _740.Solution2();
19+
solution3 = new _740.Solution3();
20+
}
1721

18-
@Test
19-
public void test1() {
20-
nums = new int[] {3, 4, 2};
21-
assertEquals(6, solution1.deleteAndEarn(nums));
22-
}
22+
@Test
23+
public void test1() {
24+
nums = new int[]{3, 4, 2};
25+
assertEquals(6, solution1.deleteAndEarn(nums));
26+
assertEquals(6, solution2.deleteAndEarn(nums));
27+
assertEquals(6, solution3.deleteAndEarn(nums));
28+
}
2329

24-
@Test
25-
public void test2() {
26-
nums = new int[] {2, 2, 3, 3, 3, 4};
27-
assertEquals(9, solution1.deleteAndEarn(nums));
28-
}
30+
@Test
31+
public void test2() {
32+
nums = new int[]{2, 2, 3, 3, 3, 4};
33+
assertEquals(9, solution1.deleteAndEarn(nums));
34+
assertEquals(9, solution2.deleteAndEarn(nums));
35+
assertEquals(9, solution3.deleteAndEarn(nums));
36+
}
2937
}

0 commit comments

Comments
 (0)