返回介绍

solution / 3000-3099 / 3014.Minimum Number of Pushes to Type Word I / README_EN

发布于 2024-06-17 01:02:57 字数 5398 浏览 0 评论 0 收藏 0

3014. Minimum Number of Pushes to Type Word I

中文文档

Description

You are given a string word containing distinct lowercase English letters.

Telephone keypads have keys mapped with distinct collections of lowercase English letters, which can be used to form words by pushing them. For example, the key 2 is mapped with ["a","b","c"], we need to push the key one time to type "a", two times to type "b", and three times to type "c" _._

It is allowed to remap the keys numbered 2 to 9 to distinct collections of letters. The keys can be remapped to any amount of letters, but each letter must be mapped to exactly one key. You need to find the minimum number of times the keys will be pushed to type the string word.

Return _the minimum number of pushes needed to type _word _after remapping the keys_.

An example mapping of letters to keys on a telephone keypad is given below. Note that 1, *, #, and 0 do not map to any letters.

 

Example 1:

Input: word = "abcde"
Output: 5
Explanation: The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.

Example 2:

Input: word = "xycdefghij"
Output: 12
Explanation: The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.

 

Constraints:

  • 1 <= word.length <= 26
  • word consists of lowercase English letters.
  • All letters in word are distinct.

Solutions

Solution 1: Greedy Algorithm

We notice that all the letters in the string $word$ are different. Therefore, we can greedily distribute the letters evenly across the $8$ keys to minimize the number of key presses.

The time complexity is $O(n / 8)$, where $n$ is the length of the string $word$. The space complexity is $O(1)$.

class Solution:
  def minimumPushes(self, word: str) -> int:
    n = len(word)
    ans, k = 0, 1
    for _ in range(n // 8):
      ans += k * 8
      k += 1
    ans += k * (n % 8)
    return ans
class Solution {
  public int minimumPushes(String word) {
    int n = word.length();
    int ans = 0, k = 1;
    for (int i = 0; i < n / 8; ++i) {
      ans += k * 8;
      ++k;
    }
    ans += k * (n % 8);
    return ans;
  }
}
class Solution {
public:
  int minimumPushes(string word) {
    int n = word.size();
    int ans = 0, k = 1;
    for (int i = 0; i < n / 8; ++i) {
      ans += k * 8;
      ++k;
    }
    ans += k * (n % 8);
    return ans;
  }
};
func minimumPushes(word string) (ans int) {
  n := len(word)
  k := 1
  for i := 0; i < n/8; i++ {
    ans += k * 8
    k++
  }
  ans += k * (n % 8)
  return
}
function minimumPushes(word: string): number {
  const n = word.length;
  let ans = 0;
  let k = 1;
  for (let i = 0; i < ((n / 8) | 0); ++i) {
    ans += k * 8;
    ++k;
  }
  ans += k * (n % 8);
  return ans;
}

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

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

发布评论

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