일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 30분
- realclass
- 리얼 클래스
- 스탭퍼
- 괜찮음
- 영어원서읽기
- 뭐든
- 사이드
- 월간
- 읽기
- 미드시청
- FIT XR
- 화상영어
- 쓰릴오브파이트
- 3줄정리
- 링피트
- 운동
- Problem Solving
- 만화도
- Writing
- 개발자
- leetcode
- 매일
- 10분
- 파비최
- English
- 영어공부
- 잡생각
- Daily Challenge
- 프로젝트
Archives
- Today
- Total
파비의 매일매일 공부기록
Today's Challenge 본문
https://leetcode.com/problems/top-k-frequent-words/
Top K Frequent Words - LeetCode
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
오랫만에 내 손으로 풀어봄.
정렬에 대한 건 살짝 리서치를 하긴 해야했지만 ㅎㅎ;
class Solution:
def topKFrequent(self, words: List[str], k: int) -> List[str]:
d = {}
res = []
for word in words:
if not d.get(word):
d[word] = 1
else:
d[word] += 1
sd = sorted(d.items(), key=lambda item: (-item[1], item[0]))
for i in range(k):
res.append(sd[i][0])
return res
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
Today's Challenge (0) | 2022.10.21 |
---|---|
Today's Challenge (0) | 2022.10.20 |
Today's Challenge (0) | 2022.10.18 |
Today's Challenge (0) | 2022.10.17 |
Today's Challenge (0) | 2022.10.16 |
Comments