-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22de77d
commit a454a63
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/com/fishercoder/solutions/fourththousand/_3016.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
src/test/java/com/fishercoder/fourththousand/_3016Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |