일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 읽기
- FIT XR
- 매일
- 프로젝트
- realclass
- 괜찮음
- 뭐든
- 3줄정리
- Writing
- 영어원서읽기
- 30분
- 개발자
- leetcode
- 영어공부
- 10분
- 스탭퍼
- 사이드
- 파비최
- 리얼 클래스
- Daily Challenge
- 잡생각
- 미드시청
- 링피트
- Problem Solving
- 만화도
- English
- 화상영어
- 월간
- 운동
- 쓰릴오브파이트
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