Skip to content

Commit a454a63

Browse files
add 3016
1 parent 22de77d commit a454a63

File tree

3 files changed

+56
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+56
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
| 3024 | [Type of Triangle](https://leetcode.com/problems/type-of-triangle/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3024.java) | | Easy |
7373
| 3028 | [Ant on the Boundary](https://leetcode.com/problems/ant-on-the-boundary/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3028.java) | | Easy |
7474
| 3019 | [Number of Changing Keys](https://leetcode.com/problems/number-of-changing-keys/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3019.java) | | Easy |
75+
| 3016 | [Minimum Number of Pushes to Type Word II](https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3016.java) | | Medium |
7576
| 3010 | [Divide an Array Into Subarrays With Minimum Cost I](https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3010.java) | | Easy |
7677
| 3014 | [Minimum Number of Pushes to Type Word I](https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3014.java) | | Easy |
7778
| 3006 | [Find Beautiful Indices in the Given Array I](https://leetcode.com/problems/find-beautiful-indices-in-the-given-array-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3006.java) | | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.PriorityQueue;
6+
7+
public class _3016 {
8+
public static class Solution1 {
9+
public int minimumPushes(String word) {
10+
Map<Character, Integer> map = new HashMap<>();
11+
for (char c : word.toCharArray()) {
12+
map.put(c, map.getOrDefault(c, 0) + 1);
13+
}
14+
PriorityQueue<Map.Entry<Character, Integer>> maxHeap = new PriorityQueue<>((a, b) -> b.getValue() - a.getValue());
15+
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
16+
maxHeap.offer(entry);
17+
}
18+
int[] possibleSets = new int[]{1, 2, 3, 4};
19+
int digitsLength = 8;//a total of 8 digits that can be assigned
20+
Map<Character, Integer> assigned = new HashMap<>();
21+
for (int j = 0; j < possibleSets.length && !maxHeap.isEmpty(); j++) {
22+
for (int i = 0; i < digitsLength && !maxHeap.isEmpty(); i++) {
23+
Map.Entry<Character, Integer> curr = maxHeap.poll();
24+
assigned.put(curr.getKey(), possibleSets[j]);
25+
}
26+
}
27+
int ans = 0;
28+
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
29+
ans += entry.getValue() * assigned.get(entry.getKey());
30+
}
31+
return ans;
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3016;
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 _3016Test {
10+
private static _3016.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3016.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(24, solution1.minimumPushes("aabbccddeeffgghhiiiiii"));
20+
}
21+
}

0 commit comments

Comments
 (0)