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