본문 바로가기
python-algorithm

leetcode 3014. Minimum Number of Pushes to Type Word I

by 무적김두칠 2024. 1. 23.

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

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
    def minimumPushes(self, word: str-> int:
        check = [[x, word.count(x)]for x in word]
        check.sort(key = lambda x : -x[1])
        eight = 0
        penalty = 1
        answer = 0
        for fre in check:
            eight += 1
            answer += fre[1]*penalty
            if eight == 8:
                penalty += 1
                eight = 0
        return answer
cs

빈도수가 높게 나오는 애들을 앞자리에 배치하고 
빈도수가 적게 나오는 애들은 뒷자리에 배치하는 방식 카운팅은 선형탐색

반응형

댓글