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

[LeetCode] 2268. Minimum Number of Keypresses

时间:2023-07-20 09:11:33浏览次数:47  
标签:map button pressing Keypresses 2268 Minimum Type 字母 once

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.

最少按键次数。

题目给了一个字符串 s,请你设计一个带有九个按键的键盘,从而使得输入 s 的按键次数最少。键盘的形式参考例子。

思路是统计 + 排序,涉及贪心的思想。首先用一个长度为 26 的数组统计每个字母的出现次数。然后我们对这个数组逆向排序,这样最多的出现次数排在前面。此时我们开始把字母往号码上填,为了使按键次数更少,我们需要将出现次数最多的字母尽可能地安排在每个号码的第一个字母位上,这样第一轮我们就会处理 9 个字母;接着我们对出现次数排名 10 - 18 这九个字母分别填在 9 个号码的第二个字母位上;剩余的字母再依次填写到每个号码的第三个字母位上。对于第一个字母位上的字母,按键一次;对于第二个字母位上的字母,按键两次;对于第三个字母位上的字母,按键三次。

时间O(nlogn)

空间O(n)

Java实现

 1 class Solution {
 2     public int minimumKeypresses(String s) {
 3         Integer[] map = new Integer[26];
 4         // 没有fill就会报错
 5         Arrays.fill(map, 0);
 6         for (int i = 0; i < s.length(); i++) {
 7             map[s.charAt(i) - 'a']++;
 8         }
 9 
10         Arrays.sort(map, (a, b) -> Integer.compare(b, a));
11         int res = 0;
12         for (int i = 0; i < 26; i++) {
13             if (i < 9) {
14                 res += map[i];
15             } else if (i >= 9 && i < 18) {
16                 res += map[i] * 2;
17             } else {
18                 res += map[i] * 3;
19             }
20         }
21         return res;
22     }
23 }

 

LeetCode 题目总结

标签:map,button,pressing,Keypresses,2268,Minimum,Type,字母,once
From: https://www.cnblogs.com/cnoodle/p/17567360.html

相关文章

  • 1851. Minimum Interval to Include Each Query (Hard)
    Description1851.MinimumIntervaltoIncludeEachQuery(Hard)Youaregivena2Dintegerarrayintervals,whereintervals[i]=[lefti,righti]describestheithintervalstartingatleftiandendingatrighti(inclusive).Thesizeofanintervalisdefi......
  • UVA10791 最小公倍数的最小和 Minimum Sum LCM 题解
    前言长沙市一中8机房0714模拟测1。传送门blog思路本题思路,首先我们先看$\operatorname{lcm}$,明显要使得这些数的$\operatorname{lcm}=n$那么我们就需要所有的数的质因子必须包含$n$的质因子。若$1\lea,b$,则$a\timesb\gea+b$,所以我们就有了策略。将同一个质因......
  • AtCoder Beginner Contest 308 G Minimum Xor Pair Query
    洛谷传送门AtCoder传送门考虑没有删除操作怎么做。可以动态维护\(ans\),新加进来一个\(x\),我们计算\(\min\limits_{y\inS}x\oplusy\)。对\(S\)建01Trie,然后从高位往低位按位贪心,在01Trie上优先往与\(x\)这一位相同的方向走,但是前提是它的子树内有数,对于01Trie......
  • node生成token报错:secretOrPrivateKey has a minimum key size of 2048 bits for RS25
    提要:在node生成token时利用用jsonwebtoken,利用非对称加密的生成token  constjwt=require("jsonwebtoken"); constprivateKey=fs.readFileSync("./keys/private.key");constpublicKey=fs.readFileSync("./keys/public.key");consttok......
  • 1595. Minimum Cost to Connect Two Groups of Points] (Hard)
    Description1595.MinimumCosttoConnectTwoGroupsofPoints(Hard)Youaregiventwogroupsofpointswherethefirstgrouphassize1points,thesecondgrouphassize2points,andsize1>=size2.Thecostoftheconnectionbetweenanytwopointsar......
  • 2712. Minimum Cost to Make All Characters Equal (Medium)
    Description2712.MinimumCosttoMakeAllCharactersEqual(Medium)Youaregivena0-indexedbinarystringsoflengthnonwhichyoucanapplytwotypesofoperations:Chooseanindexiandinvertallcharactersfromindex0toindexi(bothinclusive......
  • 2712.minimum Cost to Make All Characters Equal
    Description2712.MinimumCosttoMakeAllCharactersEqual(Medium)Youaregivena0-indexedbinarystringsoflengthnonwhichyoucanapplytwotypesofoperations:Chooseanindexiandinvertallcharactersfromindex0toindexi(bothinclusive......
  • [ABC299G]MinimumPermutation
    [ABC299G]MinimumPermutation考虑一个必要的性质:如果现在有一个数\(x_1\),它后面有一个数\(y<x_1\),且\(x_1\)又在\(y\)后面出现了若干次(\(x_2,x_3,\dots,x_k\),我们直接考虑最后一个\(x_k\)),那么\(x1\)一定不是答案。(显然前面的\(x_1\)可以不选,然后选择\(y,x_2\simx_......
  • 关于mkfs.xfs创建xfs文件系统指定block-size为512字节时报错-Minimum block size for
    今天笔者看到mkfs.xfs命令的帮助文档手册时,有如下一段内容可以通过-bsize=value的方式指定block的大小,默认值是4096bytes,最小为512,最大为65536Thedefaultvalueis4096bytes(4KiB),  theminimumis512,andthemaximumis65536(64KiB).于是笔者就尝试,创建x......
  • HDU 1394 Minimum Inversion Number(树状数组)
    题意:有一个n个整数的排列,这n个整数就是0,1,2,3...n-1这n个数(但不一定按这个顺序给出)。现在先计算一下初始排列的逆序数,然后把第一个元素a1放到an后面去,形成新排列a2a3a4...ana1,然后再求这个排列的逆序数。继续执行类似操作(一共要执行n-1次)直到产生排列ana1a2...an-1为止。......