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

边栏推荐
- Linux CentOS7安装Oracle11g的超完美新手教程
- Node——Egg 创建本地文件访问接口
- Windows installation WSL (II)
- SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】
- Kyushu cloud and Intel jointly released the smart campus private cloud framework, enabling new infrastructure for education
- Node——生成微信权限验证配置
- mysql之B tree 以及 B+tree
- SQL Server 安裝指南
- 记录一下大文件上传偶然成功偶然失败问题
- Use es to realize epidemic map or take out order function (including code and data)
猜你喜欢

GCC compilation

Database -- sqlserver details

LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
![[QT] QT cannot find a solution to the compiler using msvc2017](/img/80/a4b17d6cc1ab6fe1366a3a3f43ec2e.png)
[QT] QT cannot find a solution to the compiler using msvc2017

Openvino model performance evaluation tool DL workbench

Heketi record

SQL Server 安裝指南

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

SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】
![[cmake] cmake configuration in QT Creator](/img/e3/1cf76f88eaddb5d32184523dfb049c.png)
[cmake] cmake configuration in QT Creator
随机推荐
2023款雷克萨斯ES产品公布,这回进步很有感
GCC compilation
js 公共库 cdn 推荐
The origin of usb-if Association and various interfaces
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
Openvino model performance evaluation tool DL workbench
leetcode96不同的二叉搜索樹
leetcode96不同的二叉搜索树
USB-IF协会与各种接口的由来
Is it safe to choose mobile phone for stock trading account opening in Beijing?
SQL数据分析之子查询的综合用法和案例题【耐心整理】
Which securities company is safer to open a stock account
二叉搜索树的创建,查找,添加,删除操作
Node -- egg creates a local file access interface
数据库--SqlServer详解
Dongge cashes in and the boss retires?
【mysql 07】GPG key retrieval failed: “Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022“
Selectively inhibiting learning bias for active sampling
Guide d'installation du serveur SQL
挖财学堂开户打新债安全可靠嘛?