Skip to content

Commit efaafae

Browse files
add 3186
1 parent 1ee3fd3 commit efaafae

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

Diff for: paginated_contents/algorithms/4th_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|-----------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3186 | [Maximum Total Damage With Spell Casting](https://leetcode.com/problems/maximum-total-damage-with-spell-casting/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/_3186.java) | | Medium | DP
34
| 3185 | [Count Pairs That Form a Complete Day II](https://leetcode.com/problems/count-pairs-that-form-a-complete-day-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/_3185.java) | | Medium |
45
| 3184 | [Count Pairs That Form a Complete Day I](https://leetcode.com/problems/count-pairs-that-form-a-complete-day-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/_3184.java) | | Easy |
56
| 3178 | [Find the Child Who Has the Ball After K Seconds](https://leetcode.com/problems/find-the-child-who-has-the-ball-after-k-seconds/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/_3178.java) | | Easy |

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

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.TreeMap;
6+
7+
public class _3186 {
8+
public static class Solution1 {
9+
public long maximumTotalDamage(int[] power) {
10+
TreeMap<Integer, Integer> treeMap = new TreeMap<>();
11+
for (int p : power) {
12+
treeMap.put(p, treeMap.getOrDefault(p, 0) + 1);
13+
}
14+
List<Integer> sortedList = new ArrayList<>(treeMap.keySet());
15+
long[] dp = new long[sortedList.size()];
16+
dp[0] = (long) sortedList.get(0) * treeMap.get(sortedList.get(0));
17+
for (int i = 1; i < sortedList.size(); i++) {
18+
int currentPower = sortedList.get(i);
19+
long currentDamage = (long) currentPower * treeMap.get(currentPower);
20+
//from i - 1, all the way to the left of this sorted list, check to find the nearest valid power
21+
//using this test case: new int[]{7, 1, 6, 3}, would easily illustrate this idea
22+
//dp[i] holds the maximum possible damage for up to sortedList[i]
23+
int j = i - 1;
24+
while (j >= 0 && sortedList.get(j) >= currentPower - 2) {
25+
j--;
26+
}
27+
if (j >= 0) {
28+
dp[i] = Math.max(dp[i - 1], currentDamage + dp[j]);
29+
} else {
30+
dp[i] = Math.max(dp[i - 1], currentDamage);
31+
}
32+
}
33+
return dp[dp.length - 1];
34+
}
35+
}
36+
}

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

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._3186;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _3186Test {
10+
private static _3186.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3186.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(6, solution1.maximumTotalDamage(new int[]{1, 1, 3, 4}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(31, solution1.maximumTotalDamage(new int[]{5, 9, 2, 10, 2, 7, 10, 9, 3, 8}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(10, solution1.maximumTotalDamage(new int[]{7, 1, 6, 3}));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)