LeetCode 2268. Minimum Number of Keypresses

news/2024/10/2 6:37:48

原题链接在这里: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 }

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/44870.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

kafka事务流程

流程kafka事务使用的5个API // 1. 初始化事务 void initTransactions(); // 2. 开启事务 void beginTransaction() throws ProducerFencedException; // 3. 在事务内提交已经消费的偏移量(主要用于消费者) void sendOffsetsToTransaction(Map<TopicPartition,OffsetAndMetad…

nl80211

同 wpa_supplicant、hostapd 一样,nl80211 也可以管理无线网络,不同的是 wpa_supplicant 和 hostapd 是通过 nl80211 管理无线网络。Linux平台上目前常用的专门针对无线网络设备编程的API有两套最早的一套API由HP公司员工Jean Tourrilhes于1997年开发,全称为Linux Wireless …

C++11智能指针 unique_ptr、shared_ptr、weak_ptr、循环引用、定制删除器

目录智能指针场景引入 - 为什么需要智能指针?内存泄漏什么是内存泄漏内存泄漏的危害内存泄漏分类如何避免内存泄漏智能指针的使用及原理RAII简易例程智能指针的原理智能指针的拷贝问题智能指针的发展历史std::auto_ptr模拟实现auto_ptr例程:这种方案存在的问题:Boost库中的智能…

SingletonKit单例源码阅读学习

阅读学习QFramwork中的SingletonKit源码。 Singleton 普通类的单例 作为最常用的单例模块,通过继承单例泛型类来实现,需要私有构造;//使用第一种接口单例方式internal class Class2Singleton : Singleton<Class2Singleton>{//记录被初始化的次数private static int mI…

复习加总结

Markdown学习 标题 三级标题 四级标题 字体 粗体: 俩*hello,World 斜体:一个**hello,World* 斜体加粗三个* hello,World 删除线:两个~ hello,World 引用始作俑者没有受罚,仅仅是受害者再受害一次罢了,最多也就是管理/梦境支配者的人类在人类世界的走狗棋子被带走,毫无…

HandyControl 使用内置Command 执行无效问题

HandyControl 使用内置Command 执行无效问题HandyControl 中通过查阅代码HandyControl_Shared 共享项目中,Interactivity/Commands 目录下,存在着一些内置 Command,开心发现还有关闭窗体,最小化等系统级别常用命令。 CloseWindowCommand.cs ControlCommands.cs OpenLinkCom…

UE5——GAS实现连招的一种方案

前言 最近因为在研究多人联机同步下的动作同步,在Google上很幸运搜到了一篇日本博主写的GAS编写连招的方案,于是就打算贴出来分享一下,顺便讲讲实现的心得: 【UE5】GamePlayAbilitySystemによるコンボ攻撃の実装とそれに利用する小ネタ 前編【GAS】 【UE5】GamePlayAbilit…

cuda程序优化-3.通信简介

GPU进行卡间通信/多机通信的算法简介硬件结构 CPU<->GPU: 通过PCIe进行通信 GPU<->GPU: NVLink, 通过Switch桥接器辅助访问其他卡的HBM 多机通信: InfiniBand with GPU Direct RDMA(简称GDRDMA), 需要专用网卡卡间通信-Ring AllReduce nvidia文档 1. 初始状态卡数:…