일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- t1
- kaggle
- 43. Multiply Strings
- concurrency
- iterator
- 시바견
- Python
- 315. Count of Smaller Numbers After Self
- Python Code
- data science
- Convert Sorted List to Binary Search Tree
- attribute
- 밴픽
- 프로그래머스
- 30. Substring with Concatenation of All Words
- 109. Convert Sorted List to Binary Search Tree
- 운영체제
- Class
- Decorator
- Python Implementation
- shiba
- Substring with Concatenation of All Words
- DWG
- Protocol
- 파이썬
- 컴퓨터의 구조
- Generator
- Regular Expression
- 715. Range Module
- LeetCode
Archives
- Today
- Total
Scribbling
LeetCode: 347. Top K Frequent Elements 본문
빈도를 기억하기 위한 hashmap을 사용하자.
from collections import defaultdict
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
dic = defaultdict(int)
for num in nums:
dic[num] += 1
temp = []
for key, val in dic.items():
temp.append([val, key])
temp.sort(key=lambda x: -x[0])
temp = temp[:k]
return [t[1] for t in temp]
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 138. Copy List with Random Pointer (0) | 2021.11.16 |
---|---|
LeetCode: 295. Find Median from Data Stream (0) | 2021.11.16 |
LeetCode: 354. Russian Doll Envelopes (0) | 2021.11.15 |
LeetCode: 300. Longest Increasing Subsequence (0) | 2021.11.15 |
LeetCode: 132. Palindrome Partitioning II (0) | 2021.11.13 |