Skip to content

Commit 6c8ca95

Browse files
solve Minimum Number of Pushes to Type Word I in java
1 parent abeb834 commit 6c8ca95

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@
883883
| 3000 | [Maximum Area of Longest Diagonal Rectangle](https://leetcode.com/problems/maximum-area-of-longest-diagonal-rectangle) | [![Java](assets/java.png)](src/MaximumAreaOfLongestDiagonalRectangle.java) | |
884884
| 3005 | [Count Elements With Maximum Frequency](https://leetcode.com/problems/count-elements-with-maximum-frequency) | [![Java](assets/java.png)](src/CountElementsWithMaximumFrequency.java) | |
885885
| 3010 | [Divide an Array Into Subarrays With Minimum Cost I](https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i) | [![Java](assets/java.png)](src/DivideAnArrayIntoSubarraysWithMinimumCostI.java) | |
886-
| 3014 | [Minimum Number of Pushes to Type Word I](https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-i) | | |
886+
| 3014 | [Minimum Number of Pushes to Type Word I](https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-i) | [![Java](assets/java.png)](src/MinimumNumberOfPushesToTypeWordI.java) | |
887887
| 3019 | [Number of Changing Keys](https://leetcode.com/problems/number-of-changing-keys) | | |
888888
| 3024 | [Type of Triangle](https://leetcode.com/problems/type-of-triangle) | | |
889889
| 3028 | [Ant on the Boundary](https://leetcode.com/problems/ant-on-the-boundary) | | |
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-i
2+
// T: O(|word|)
3+
// S: O(|word|)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class MinimumNumberOfPushesToTypeWordI {
9+
public static int minimumPushes(String word) {
10+
final Map<Character, Integer> letterFrequency = getLetterFrequencies(word);
11+
final var entries = letterFrequency.entrySet().stream().sorted((a, b) -> Integer.compare(b.getValue(), a.getValue())).toList();
12+
13+
int pushes = 0;
14+
for (int i = 0 ; i < entries.size() ; i++) {
15+
pushes += entries.get(i).getValue() * ((i + 8) / 8);
16+
}
17+
18+
return pushes;
19+
}
20+
21+
private static Map<Character, Integer> getLetterFrequencies(String s) {
22+
final Map<Character, Integer> frequencies = new HashMap<>();
23+
for (int i = 0 ; i < s.length() ; i++) {
24+
frequencies.put(s.charAt(i), frequencies.getOrDefault(s.charAt(i), 0) + 1);
25+
}
26+
return frequencies;
27+
}
28+
}

0 commit comments

Comments
 (0)