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 |
빈도수가 높게 나오는 애들을 앞자리에 배치하고
빈도수가 적게 나오는 애들은 뒷자리에 배치하는 방식 카운팅은 선형탐색
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 3019. Number of Changing Keys (0) | 2024.02.05 |
---|---|
백준 9063 대지 (0) | 2024.01.24 |
leetcode 3010. Divide an Array Into Subarrays With Minimum Cost I (0) | 2024.01.23 |
leetcode 645. Set Mismatch (0) | 2024.01.22 |
leetcode 1207. Unique Number of Occurrences (0) | 2024.01.17 |
댓글