일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 스탭퍼
- leetcode
- FIT XR
- 30분
- 괜찮음
- 잡생각
- 3줄정리
- Problem Solving
- Writing
- 만화도
- 개발자
- English
- 영어공부
- 운동
- 10분
- 뭐든
- 링피트
- 프로젝트
- 읽기
- Daily Challenge
- 미드시청
- 매일
- 화상영어
- 영어원서읽기
- 쓰릴오브파이트
- 파비최
- 리얼 클래스
- realclass
- 월간
- 사이드
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.05.22 Today's Challenge 본문
https://leetcode.com/problems/top-k-frequent-elements/
Top K Frequent Elements - LeetCode
Can you solve this real interview question? Top K Frequent Elements - Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2]
leetcode.com
heapq의 nlargest 함수를 이용해서 풀면 간단히 풀림.
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
if k == len(nums):
return nums
cnt = Counter(nums)
return heapq.nlargest(k, cnt.keys(), key=cnt.get)
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.05.24 Today's Challenge (0) | 2023.05.24 |
---|---|
2023.05.23 Today's Challenge (0) | 2023.05.23 |
2023.05.21 Today's Challenge (1) | 2023.05.21 |
2023.05.20 Today's Challenge (0) | 2023.05.20 |
2023.05.19 Today's Challenge (0) | 2023.05.19 |
Comments