Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- binary search
- 상가수익률계산기
- 파이썬 알고리즘 풀기
- 파이썬알고리즘풀기
- 릿코드풀이
- 파이썬릿코드
- 파이썬 릿코드
- 파이썬 알고리즘
- leetcode풀기
- python sorted
- leetcode풀이
- python 릿코드
- 알고리즘풀이
- 릿코드 파이썬
- leetcode 풀기
- python xor
- 릿코드
- 릿코드 풀기
- 파이썬 프로그래머스
- python zip_longest
- python Leetcode
- 알고리즘풀기
- 릿코드풀기
- python 알고리즘
- LeetCode
- python priority queue
- 파이썬릿코드풀기
- 코틀린기초
- 파이썬알고리즘
- 잇츠디모
Archives
- Today
- Total
소프트웨어에 대한 모든 것
LeetCode 풀기 - 347. Top K Frequent Elements 본문
반응형
347. Top K Frequent Elements
https://leetcode.com/problems/top-k-frequent-elements/
Top K Frequent Elements - 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
문제)
솔루션1) Counter 객체 사용
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
counter = Counter(nums)
return list(zip(*counter.most_common(k)))[0]
솔루션2) sorted() 사용
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
counter = Counter(nums)
nums = list(counter.items())
# count 내림차순 정렬
nums = sorted(nums, key=lambda x: -x[1])
nums = nums[:k]
return list(zip(*nums))[0]
함께 보면 좋은 글:
반응형
'알고리즘 > LeetCode' 카테고리의 다른 글
LeetCode 풀기 - 49. Group Anagrams (0) | 2022.02.26 |
---|---|
LeetCode 풀기 - 2169. Count Operations to Obtain Zero (0) | 2022.02.26 |
LeetCode 풀기 - 165. Compare Version Numbers (0) | 2022.02.25 |
LeetCode 풀기 - 121. Best Time to Buy and Sell Stock (0) | 2022.02.25 |
LeetCode 풀기 - 215. Kth Largest Element in an Array (0) | 2022.02.24 |
Comments