返回介绍

solution / 2200-2299 / 2268.Minimum Number of Keypresses / README_EN

发布于 2024-06-17 01:03:08 字数 4207 浏览 0 评论 0 收藏 0

2268. 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.

Solutions

Solution 1

class Solution:
  def minimumKeypresses(self, s: str) -> int:
    cnt = Counter(s)
    ans = 0
    i, j = 0, 1
    for v in sorted(cnt.values(), reverse=True):
      i += 1
      ans += j * v
      if i % 9 == 0:
        j += 1
    return ans
class Solution {
  public int minimumKeypresses(String s) {
    int[] cnt = new int[26];
    for (char c : s.toCharArray()) {
      ++cnt[c - 'a'];
    }
    Arrays.sort(cnt);
    int ans = 0;
    for (int i = 1, j = 1; i <= 26; ++i) {
      ans += j * cnt[26 - i];
      if (i % 9 == 0) {
        ++j;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int minimumKeypresses(string s) {
    vector<int> cnt(26);
    for (char& c : s) ++cnt[c - 'a'];
    sort(cnt.begin(), cnt.end());
    int ans = 0;
    for (int i = 1, j = 1; i <= 26; ++i) {
      ans += j * cnt[26 - i];
      if (i % 9 == 0) ++j;
    }
    return ans;
  }
};
func minimumKeypresses(s string) int {
  cnt := make([]int, 26)
  for _, c := range s {
    cnt[c-'a']++
  }
  sort.Ints(cnt)
  ans := 0
  for i, j := 1, 1; i <= 26; i++ {
    ans += j * cnt[26-i]
    if i%9 == 0 {
      j++
    }
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文