Skip to content

Commit

Permalink
add 3016
Browse files Browse the repository at this point in the history
  • Loading branch information
fishercoder1534 committed Aug 6, 2024
1 parent 22de77d commit a454a63
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions paginated_contents/algorithms/4th_thousand/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/fishercoder/solutions/fourththousand/_3016.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.fishercoder.solutions.fourththousand;

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class _3016 {
public static class Solution1 {
public int minimumPushes(String word) {
Map<Character, Integer> map = new HashMap<>();
for (char c : word.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
PriorityQueue<Map.Entry<Character, Integer>> maxHeap = new PriorityQueue<>((a, b) -> b.getValue() - a.getValue());
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
maxHeap.offer(entry);
}
int[] possibleSets = new int[]{1, 2, 3, 4};
int digitsLength = 8;//a total of 8 digits that can be assigned
Map<Character, Integer> assigned = new HashMap<>();
for (int j = 0; j < possibleSets.length && !maxHeap.isEmpty(); j++) {
for (int i = 0; i < digitsLength && !maxHeap.isEmpty(); i++) {
Map.Entry<Character, Integer> curr = maxHeap.poll();
assigned.put(curr.getKey(), possibleSets[j]);
}
}
int ans = 0;
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
ans += entry.getValue() * assigned.get(entry.getKey());
}
return ans;
}
}
}
21 changes: 21 additions & 0 deletions src/test/java/com/fishercoder/fourththousand/_3016Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.fishercoder.fourththousand;

import com.fishercoder.solutions.fourththousand._3016;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class _3016Test {
private static _3016.Solution1 solution1;

@BeforeEach
public void setup() {
solution1 = new _3016.Solution1();
}

@Test
public void test1() {
assertEquals(24, solution1.minimumPushes("aabbccddeeffgghhiiiiii"));
}
}

0 comments on commit a454a63

Please sign in to comment.