-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeetcode2268.java
30 lines (30 loc) · 960 Bytes
/
Leetcode2268.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Leetcode2268 {
public int minimumKeypresses(String s) {
int[] freq = new int[26];
// check the frequency of each character
for(int i = 0;i < s.length();i++){
freq[s.charAt(i)-'a']++;
}
// the character with most frequency should have least press time
PriorityQueue<int[]> q = new PriorityQueue<>((a,b)->b[1]-a[1]);
for(int i = 0;i < 26;i++){
q.offer(new int[]{i,freq[i]});
}
int[] pressTime = new int[26];
for(int i = 0;i < 26;i++){
int[] cur = q.poll();
if(i < 9){
pressTime[cur[0]] = 1;
}else if(i >= 9 && i < 18){
pressTime[cur[0]] = 2;
}else{
pressTime[cur[0]] = 3;
}
}
int res = 0;
for(int i = 0;i < s.length();i++){
res += pressTime[s.charAt(i)-'a'];
}
return res;
}
}