diff --git a/paginated_contents/algorithms/4th_thousand/README.md b/paginated_contents/algorithms/4th_thousand/README.md index e8a7b7c61b..031d2949dc 100644 --- a/paginated_contents/algorithms/4th_thousand/README.md +++ b/paginated_contents/algorithms/4th_thousand/README.md @@ -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 | diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3016.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3016.java new file mode 100644 index 0000000000..4ab9953209 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3016.java @@ -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 map = new HashMap<>(); + for (char c : word.toCharArray()) { + map.put(c, map.getOrDefault(c, 0) + 1); + } + PriorityQueue> maxHeap = new PriorityQueue<>((a, b) -> b.getValue() - a.getValue()); + for (Map.Entry 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 assigned = new HashMap<>(); + for (int j = 0; j < possibleSets.length && !maxHeap.isEmpty(); j++) { + for (int i = 0; i < digitsLength && !maxHeap.isEmpty(); i++) { + Map.Entry curr = maxHeap.poll(); + assigned.put(curr.getKey(), possibleSets[j]); + } + } + int ans = 0; + for (Map.Entry entry : map.entrySet()) { + ans += entry.getValue() * assigned.get(entry.getKey()); + } + return ans; + } + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3016Test.java b/src/test/java/com/fishercoder/fourththousand/_3016Test.java new file mode 100644 index 0000000000..9bb4ada065 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3016Test.java @@ -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")); + } +} \ No newline at end of file