Skip to content

3016. Minimum Number of Pushes to Type Word II

#278

Problem link

https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-ii/

Problem Summary

주어진 문자열을 키패드로 입력할 때 버튼 누르는 횟수의 최소를 구하는 문제. 키패드는 자유롭게 배치할 수 있다.

Solution

간단한 문제.
문자열의 문자들의 개수를 센 뒤, 개수가 많은 문자를 키패드의 앞쪽으로 배치하면 된다.

Source Code

class Solution:
    def minimumPushes(self, word: str) -> int:
        cnt = Counter(word)
        ans = 0
        alphabet = 0
        for a, c in cnt.most_common():
            ans += (alphabet // 8 + 1) * c
            alphabet += 1
        return ans