当前位置:网站首页>Leetcode skimming: stack and queue 06 (top k high-frequency elements)
Leetcode skimming: stack and queue 06 (top k high-frequency elements)
2022-07-02 00:26:00 【Taotao can't learn English】
347. front K High frequency elements
Given a non empty array of integers , Before returning the frequency of occurrence k High element .
Example 1:
- Input : nums = [1,1,1,2,2,3], k = 2
- Output : [1,2]
Example 2:
- Input : nums = [1], k = 1
- Output : [1]
Tips :
- You can assume that given k Always reasonable , And 1 ≤ k ≤ The number of different elements in the array .
- The time complexity of your algorithm must be better than O ( n log n ) O(n \log n) O(nlogn) , n Is the size of the array .
- The question data guarantee the unique answer , let me put it another way , Before array k The set of high-frequency elements is unique .
- You can return the answers in any order .
It's another medium-sized problem, and one hit the soul , Gone with the wind hhhhh
Ideas :
The hash table calculates the number of occurrences of each number
key 、 Values are put into two arrays , Sort by value , Key image synchronous transformation position
Return to the former K A digital .
package com.programmercarl.stacks_queues;
import java.util.HashMap;
/** * @ClassName TopKFrequent * @Descriotion TODO * @Author nitaotao * @Date 2022/6/30 14:13 * @Version 1.0 * https://leetcode.cn/problems/top-k-frequent-elements/ * 347. front K High frequency elements **/
public class TopKFrequent {
public static int[] topKFrequent(int[] nums, int k) {
/** * Ideas : * Hash table count */
HashMap<Integer, Integer> map = new HashMap();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
map.put(nums[i], map.get(nums[i]) + 1);
} else {
map.put(nums[i], 1);
}
}
int[] keys = new int[map.size()];
int[] values = new int[map.size()];
int index = 0;
for (Integer key : map.keySet()) {
keys[index] = key;
values[index] = map.get(key);
index++;
}
//value Sort ,key Image synchronization Exchange
for (int i = 0; i < values.length-1; i++) {
for (int j = i+1; j < values.length; j++) {
if (values[i] < values[j]) {
int tempValue = values[i];
values[i] = values[j];
values[j] = tempValue;
int tempKey = keys[i];
keys[i] = keys[j];
keys[j] = tempKey;
}
}
}
int[] result = new int[k];
for (int i = 0; i < k; i++) {
result[i] = keys[i];
}
return result;
}
}

There is another way to look at the solution , It's the train of thought that our teacher said in class before , Big top pile idea .
public static int[] topKFrequent(int[] nums, int k) {
/** * Ideas : * Hash table count */
HashMap<Integer, Integer> map = new HashMap();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
map.put(nums[i], map.get(nums[i]) + 1);
} else {
map.put(nums[i], 1);
}
}
// Big top pile idea
// Priority queue , Sort by value size
PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>((o1, o2) -> o1.getValue() - o2.getValue());
for(Map.Entry<Integer,Integer>entry : map.entrySet()) {
queue.offer(entry);
// Before retention only k Number
if (queue.size() > k) {
queue.poll();
}
}
int[] result = new int[k];
for (int i = 0; i < k; i++) {
result[i] = queue.poll().getKey();
}
return result;
}

边栏推荐
- Accelerator systems initiative is an independent non-profit organization
- SQL数据分析之流程控制语句【if,case...when详解】
- Node——Egg 实现上传文件接口
- 牛客-练习赛101-推理小丑
- When installing mysql, there are two packages: Perl (data:: dumper) and Perl (JSON)
- 2022拼多多详情/拼多多商品详情/拼多多sku详情
- Which app is better and more secure for stock mobile account opening
- Export default the exported object cannot be deconstructed, and module Differences between exports
- 挖财学堂开户打新债安全可靠嘛?
- Learn online case practice
猜你喜欢
![[embedded system course design] a single key controls the LED light](/img/c9/076618208bbab0b95faa5a7e644a07.png)
[embedded system course design] a single key controls the LED light
Linux centos7 installation Oracle11g super perfect novice tutorial

4. Object mapping Mapstercover

LDR6035智能蓝牙音响可对手机设备持续充放电方案
![Jielizhi, production line assembly link [chapter]](/img/1d/d1736fad33c428e61f450aad512ce0.png)
Jielizhi, production line assembly link [chapter]

回顾数据脱敏系统

PWN attack and defense world cgpwn2

Intelligent operation and maintenance practice: banking business process and single transaction tracking

UDS bootloader of s32kxxx bootloader

Pytorch learning record
随机推荐
Jielizhi Bluetooth headset quality control and production skills [chapter]
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
【CTF】bjdctf_2020_babystack2
13 MySQL constraint
Use es to realize epidemic map or take out order function (including code and data)
Export default the exported object cannot be deconstructed, and module Differences between exports
Download the online video m3u8 tutorial
【CTF】bjdctf_2020_babystack2
Timer和ScheduledThreadPoolExecutor的区别
JS common library CDN recommendation
Graduation season is both a farewell and a new beginning
Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]
Review data desensitization system
The difference between timer and scheduledthreadpoolexecutor
js 公共库 cdn 推荐
Material design component - use bottomsheet to show extended content (I)
How to improve data quality
[QT] qtcreator uninstall and installation (abnormal state)
JS -- image to base code, base to file object
Leetcode medium question sharing (5)