| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- iterator
- 밴픽
- 109. Convert Sorted List to Binary Search Tree
- 컴퓨터의 구조
- 파이썬
- 315. Count of Smaller Numbers After Self
- Regular Expression
- 30. Substring with Concatenation of All Words
- 운영체제
- 43. Multiply Strings
- Generator
- Python Code
- concurrency
- Class
- t1
- attribute
- LeetCode
- Protocol
- 프로그래머스
- DWG
- Python
- Substring with Concatenation of All Words
- Python Implementation
- shiba
- kaggle
- 시바견
- Decorator
- 715. Range Module
- Convert Sorted List to Binary Search Tree
- data science
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 |