首页 > 其他分享 >LeetCode 2268. Minimum Number of Keypresses

LeetCode 2268. Minimum Number of Keypresses

时间:2024-06-17 21:12:21浏览次数:23  
标签:int button pressing Keypresses 2268 Minimum each Type once

原题链接在这里:https://leetcode.com/problems/minimum-number-of-keypresses/description/

题目:

You have a keypad with 9 buttons, numbered from 1 to 9, each mapped to lowercase English letters. You can choose which characters each button is matched to as long as:

  • All 26 lowercase English letters are mapped to.
  • Each character is mapped to by exactly 1 button.
  • Each button maps to at most 3 characters.

To type the first character matched to a button, you press the button once. To type the second character, you press the button twice, and so on.

Given a string s, return the minimum number of keypresses needed to type s using your keypad.

Note that the characters mapped to by each button, and the order they are mapped in cannot be changed.

Example 1:

Input: s = "apple"
Output: 5
Explanation: One optimal way to setup your keypad is shown above.
Type 'a' by pressing button 1 once.
Type 'p' by pressing button 6 once.
Type 'p' by pressing button 6 once.
Type 'l' by pressing button 5 once.
Type 'e' by pressing button 3 once.
A total of 5 button presses are needed, so return 5.

Example 2:

Input: s = "abcdefghijkl"
Output: 15
Explanation: One optimal way to setup your keypad is shown above.
The letters 'a' to 'i' can each be typed by pressing a button once.
Type 'j' by pressing button 1 twice.
Type 'k' by pressing button 2 twice.
Type 'l' by pressing button 3 twice.
A total of 15 button presses are needed, so return 15.

Constraints:

  • 1 <= s.length <= 105
  • s consists of lowercase English letters.

题解:

Need to put the most frequent chars in the front of each button.

We put the 9 most frequent chars at the beginning of each button.

Then next 9 most frequent chars at the second of each button.

Time Complexity: O(n). n = s.length().

Space: O(1).

AC Java:

 1 class Solution {
 2     public int minimumKeypresses(String s) {
 3         int[] count = new int[26];
 4         for(int i = 0; i < s.length(); i++){
 5             count[s.charAt(i) - 'a']++;
 6         }
 7 
 8         Arrays.sort(count);
 9         int res = 0;
10         int ind = 0;
11         for(int i = 25; i >= 0; i--){
12             res += count[i] * (ind / 9 + 1);
13             ind++;
14         }
15 
16         return res;
17     }
18 }

 

标签:int,button,pressing,Keypresses,2268,Minimum,each,Type,once
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18253198

相关文章

  • LeetCode 2340. Minimum Adjacent Swaps to Make a Valid Array
    原题链接在这里:https://leetcode.com/problems/minimum-adjacent-swaps-to-make-a-valid-array/description/题目:Youaregivena 0-indexed integerarray nums.Swaps of adjacent elementsareabletobeperformedon nums.A valid arraymeetsthefollowingco......
  • 209. Minimum Size Subarray Sum
    Givenanarrayofpositiveintegersnumsandapositiveintegertarget,returntheminimallengthofasubarraywhosesumisgreaterthanorequaltotarget.Ifthereisnosuchsubarray,return0instead.Example1:Input:target=7,nums=[2,3,1,2,......
  • P9327 [CCC 2023 S4] Minimum Cost Roads
    原题链接题解贪心,我管这种叫做策略贪心,即按照某种顺序或者角度去贪心可以得到最优解既然题目要求任意两点间最短路最小的同时,价格也最小,那么我们就按长度为第一关键字,花费为第二关键字排序。然后遍历所有边看看这条边能否使用遍历过程的策略:如果这条边加入后,这条边两端的节点......
  • CF1085D Minimum Diameter Tree 题解
    CF1085DMinimumDiameterTree题解比较水的一道绿题观察样例可以发现,边权都平分在叶子节点唯一的一条连边上,由此猜到联想到可以把贪心地将边权全部平均分配到这些边上,这样写出来就能AC了。如何证明先来一张图方便理解:利用反证法:假设按上述做法分配边权后可以至少修改一次......
  • [LeetCode] Find the Minimum Cost Array Permutation
    Youaregivenanarray nums whichisa permutation of [0,1,2,...,n-1].The score ofanypermutationof [0,1,2,...,n-1] named perm isdefinedas:score(perm)=|perm[0]-nums[perm[1]]|+|perm[1]-nums[perm[2]]|+...+|perm[n-1]-......
  • 64 - Minimum Path Sum 最小路径和
    64-MinimumPathSum最小路径和问题描述Givenamxngridfilledwithnon-negativenumbers,findapathfromtoplefttobottomright,whichminimizesthesumofallnumbersalongitspath.Note:Youcanonlymoveeitherdownorrightatanypointinti......
  • 题解 P9981 [USACO23DEC] Minimum Longest Trip G
    【洛谷专栏】题意\(N\)个点\(M\)条边的有向无环图,对于每一个点\(i\)你需要求出一条从\(i\)出发的最长路径且路径上边权组成的序列字典序最小。求每一条路径的长度和边权和。分析最长的路径很好求,在DAG上拓扑排序后动态规划即可。(具体的实现可以参考OI-Wiki)现在的......
  • [ABC297F] Minimum Bounding Box 2 题解
    容斥真有趣。有一个性质:两个相同的子矩阵,对答案的贡献一定相同。所以就只需要枚举矩阵大小即可。我们设当前矩阵长\(i\)宽\(j\)(对应的,\(H\)为长,\(W\)为宽),假如要给答案做出贡献,矩阵的四条边一定都有点,发现可以容斥了。至少\(0\)条边上有点的方案数为\(a=C_{i\times......
  • 「AGC005B」 Minimum Sum
    题意给定一个整数序列\(a\),求\(\sum\limits^{N}_{l=1}\sum\limits^{N}_{r=l}\min(a_l,a_{l+1},\cdots,a_{r})\)(注意\(r\)的初始值是\(l\))。分析当我们模拟样例时,可以发现,每一个数都只会在一个区间内最小,而最小值不断更新成更小的,所以可以用单调栈求解出\(a_i\)对应......
  • CF1771F Hossam and Range Minimum Query 题解
    题目链接:CF或者洛谷比较不错的题,出现奇数次出现的这种问题,比较容易想到一种运算,那就是异或和运算。如果一个区间上一个数出现偶数次,则它对于异或和的贡献为\(0\),那么很显然,我们维护下区间异或和即可判断一个区间上是否存在出现奇数次的数。然后我们注意到\(1\oplus2\oplu......