当前位置:网站首页>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;
}

边栏推荐
- 【QT】测试Qt是否能连接上数据库
- An intern's journey to cnosdb
- Barbie q! How to analyze the new game app?
- [QT] qtcreator uninstall and installation (abnormal state)
- Operate database transactions with jpatractionmanager
- Practical calculation of the whole process of operational amplifier hysteresis comparator
- Download the online video m3u8 tutorial
- Ldr6035 smart Bluetooth audio can continuously charge and discharge mobile devices
- JS common library CDN recommendation
- GCC compilation
猜你喜欢

Openvino model performance evaluation tool DL workbench

Asp .NetCore 微信订阅号自动回复之文本篇

Heketi record

Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?

【QT】Qt 使用MSVC2017找不到编译器的解决办法
![Jielizhi, production line assembly link [chapter]](/img/f8/20c41ffe9468d59bf25ea49f73751e.png)
Jielizhi, production line assembly link [chapter]

B tree and b+tree of MySQL

【QT】对于Qt MSVC 2017无法编译的问题解决

The origin of usb-if Association and various interfaces

关联性——组内相关系数
随机推荐
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
leetcode96不同的二叉搜索樹
Resumption of attack and defense drill
[cmake] cmake configuration in QT Creator
leetcode96不同的二叉搜索树
九州云与英特尔联合发布智慧校园私有云框架,赋能教育新基建
Node——添加压缩文件
Is it safe for qiniu college to open an account? How to open an account?
【QT】Qt 使用MSVC2017找不到编译器的解决办法
回顾数据脱敏系统
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
Windows10 install WSL (I) (wslregisterdistribution error)
[CTF] bjdctf 2020 Bar _ Bacystack2
Selectively inhibiting learning bias for active sampling
关联性——组内相关系数
Timer和ScheduledThreadPoolExecutor的区别
From 20s to 500ms, I used these three methods
Graduation season is both a farewell and a new beginning
起床困难综合症(按位贪心)