leetcode 895. Maximum Frequency Stack(最大频率栈)

【栈】895. 最大频率栈|2027 设计一个类似堆栈的数据结构,将元素推入堆栈,并从堆栈中弹出出现频率最高的元素。 实现 FreqStack 类: FreqStack() 构造一个空的堆栈。 void push(int val) 将一个整数 val 压入栈顶。 int pop() 删除并返回堆栈中出现频率最高的元素。 如果出现频率最高的元素不只一个,则移除并返回最接近栈顶的元素。 阅读详情

Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.

Implement the FreqStack class:

FreqStack() constructs an empty frequency stack.
void push(int val) pushes an integer val onto the top of the stack.
int pop() removes and returns the most frequent element in the stack.
If there is a tie for the most frequent element, the element closest to the stack’s top is removed and returned.

Example 1:

Input
[“FreqStack”, “push”, “push”, “push”, “push”, “push”, “push”, “pop”, “pop”, “pop”, “pop”]
[[], [5], [7], [5], [7], [4], [5], [], [], [], []]
Output
[null, null, null, null, null, null, null, 5, 7, 5, 4]

Explanation
FreqStack freqStack = new FreqStack();
freqStack.push(5); // The stack is [5]
freqStack.push(7); // The stack is [5,7]
freqStack.push(5); // The stack is [5,7,5]
freqStack.push(7); // The stack is [5,7,5,7]
freqStack.push(4); // The stack is [5,7,5,7,4]
freqStack.push(5); // The stack is [5,7,5,7,4,5]
freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,5,7,4].
freqStack.pop(); // return 7, as 5 and 7 is the most frequent, but 7 is closest to the top. The stack becomes [5,7,5,4].
freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,4].
freqStack.pop(); // return 4, as 4, 5 and 7 is the most frequent, but 4 is closest to the top. The stack becomes [5,7].

写一个返回最大频率元素的stack。

思路:
既然是stack就要满足后进先出,还要记录频率。

那么需要一个变量maxFreq记录最大频率,
一个HashMap记录每个数字对应的出现频率。
当pop时,需要知道maxFreq对应的有哪些元素,及元素的push的先后顺序。
所以还需要一个HashMap用来保存每个出现频率对应的有哪些元素,这些元素要按push的顺序保存。

push时,首先要给对应数字的频率+1;该频率对应的list中加入push的数字。
然后更新maxFreq。
(注意:比如5出现了3次,那么map中(频率,数字list)对中要保存(1, [5]), (2, [5]), (3, [5]))

pop时,因为知道maxFreq,所以直接从HashMap中找到对应的数字list,
list中的数字是按push的顺序保存的,所以直接返回最后一个数字,并删除它;该数字对应的频率也要-1。
当list为空时,说明该maxFreq对应的已经没有数字了,maxFreq要-1。

class FreqStack {
    HashMap<Integer, List<Integer>> map = new HashMap<>();
    HashMap<Integer, Integer> freq = new HashMap<>();
    int maxFreq = 0;
    int fq = 0;
    
    public FreqStack() {
        
    }
    
    public void push(int val) {
        freq.put(val, freq.getOrDefault(val, 0) + 1);
        fq = freq.get(val);
        
        maxFreq = Math.max(maxFreq, fq);
        if(!map.containsKey(fq)) {
            map.put(fq, new ArrayList<Integer>());
        }
        map.get(fq).add(val);
    }
    
    public int pop() {
        int len = map.get(maxFreq).size();
        int element = map.get(maxFreq).remove(len - 1);
        freq.put(element, freq.get(element) - 1);
        if(len == 1) {
            maxFreq --;
        }
        return element;
    }
}
别再只会regress了!用Stata的csdid命令搞定多期DID的完整实操指南 本文详细介绍了如何在Stata中使用csdid命令进行多期DID分析,解决传统DID方法在交错处理场景下的局限性。通过完整的实操指南,包括环境搭建、核心语法、结果解读和高阶应用,帮助研究者更准确地评估政策效果,适用于计量经济学和实证研究领域。 阅读详情

相关推荐

DL-FWI基础入门培训视频学习1、2

DL-FWI基础知识

shengMio的博客 723

leetcode_题解_min stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get

james的专栏 504

”COMSOL“竟然如此简单!我悟了!!

COMSOL Multiphysics具有强大的多物理场全耦合仿真分析功能、高效的计算性能,可以保证数值仿真的高度精确,已被广泛应用于各个学科领域,近年来运用COMSOL来解决电化学实际工程问题也越来越普遍。

C1012562281的博客 3462

LeetCode 最大频率栈(hash表+stack、优先队列)

实现 FreqStack,模拟类似栈的数据结构的操作的一个类。 FreqStack 有两个函数: push(int x),将整数 x 推入栈中。 pop(),它移除并返回栈中出现最频繁的元素。 如果最频繁的元素不只一个,则移除并返回最接近栈顶的元素。 示例: 输入: ["FreqStack","push","push","push","push","push","push","pop","pop...

hestyle的博客 1125

jmeter问题系列:Begin size 0 is not equal to fixed size 5

Begin size 0 is not equal to fixed size 5 ,问题的解决

雨水的早晨的博客 2425

【视频讲解】Python量子计算聚类Q-means:量子k-means算法分析电路数据实现可视化...

全文链接:https://tecdat.cn/?p=37821分析师:Yifan Zhang量子计算在近期已然成为一个频繁出现的热门概念。尽管它在大众认知以及互联网社区中备受瞩目,热度极高,然而就其实际能力而言,当前仍然存在诸多局限(点击文末“阅读原文”获取完整代码数据)。量子计算作为一个全新的领域,带来了相较于传统经典计算模式的重大范式转变。在量子计算的体系中,传统的 0 或 1 的经典比特被一...

拓端研究室TRL 1464

【895. 最大频率栈】

在本题中,每次需要优先弹出频率最大的元素,如果频率最大元素有多个,则优先弹出靠近栈顶的元素。因此,我们可以考虑将栈序列分解为多个频率不同的栈序列,每个栈维护同一频率的元素。当元素入栈时频率增加,将元素加入到更高频率的栈中,低频率栈中的元素不需要出栈。当元素出栈时,将频率最高的栈的栈顶元素出栈即可。设当前最大频率为 maxFreq ,为 1 ∼ maxFreq 中的每种频率单独设置一个栈。为了方便描述,记 freq[x] 为 x 的频率,group[i] 为频率为 i 的栈。

Sugar_wolf的博客 413

SRC漏洞挖掘实战:从攻击面管理到业务逻辑漏洞的精准狩猎

在网络安全领域,漏洞挖掘是识别和利用软件系统中安全弱点的核心实践。其基本原理在于通过系统化的方法,识别和评估一个应用或系统的攻击面,并从中定位安全防护的薄弱环节。这项技术的价值在于能够主动发现潜在风险,防患于未然,对于保障企业数字资产安全至关重要。典型的应用场景包括对Web应用、移动端API以及各类在线业务系统的安全性评估。在实际的漏洞挖掘,尤其是针对安全应急响应中心(SRC)的实战中,高效的策略往往聚焦于**边缘资产**的发现与利用,以及对**业务逻辑漏洞**的模式化测试。掌握这些经过验证的思路,能帮助安

babing2770的博客 425

895. 最大频率栈

问题描述 最大频率栈 实现 FreqStack,模拟类似栈的数据结构的操作的一个类。 FreqStack 有两个函数: push(int x),将整数 x 推入栈中。 pop(),它移除并返回栈中出现最频繁的元素。 如果最频繁的元素不只一个,则移除并返回最接近栈顶的元素。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximum-frequency-stack 解题思路 这题依然主要是按照频率和按照时间排序,和之前写的LFU 算法c++的

超级码力 681

STM32CubeMX、keil、simulink联合开发MCU教程

一、整体思路 利用Matlab的simulink搭建模型生成C代码,通过stm32cubemx生成工程,最后在KEIL或者IAR等工具里面编译生成代码下载至MCU中执行。 本文以流水灯建立模型和简单使用定时器为例进行演示。 二、开发环境 1、安装Matlab Matlab版本建议在2013B以上,本文使用Matlab 2016a。 2、安装STM32硬件支持包 下载地址(https://www.stmcu.org.cn/document/detail/index/id-216053) 安装完成后重启MAT

tzlaa的博客 7764

企业数据治理体系中的AI与业务融合:架构师的3个成功案例

企业数据治理的痛点从来不是“有没有做”,而是“做了之后有没有用”——很多企业花了数百万建数据仓库、做元数据管理,最后却陷入“数据躺在库里睡大觉”“AI模型因数据质量差而失效”的困境。本文通过零售、金融、制造3个真实案例,拆解架构师如何用AI连接数据治理与业务场景:从“客户画像不准”到“精准推荐提升30%转化率”,从“欺诈检测漏报”到“实时拦截减少50%损失”,从“库存积压”到“供应链周转率提升20%”。如何让数据治理从「合规驱动」转向「价值驱动」?

AI 原生应用开发的博客 996

[LeetCode] 895. Maximum Frequency Stack 最大频率栈

ImplementFreqStack, a class which simulates the operation of a stack-like data structure. FreqStackhas two functions: push(int x), which pushes an integerxonto the stack. pop(), whichremovesand...

weixin_30393907的博客 269

微信小程序 - request:fail errcode:10011 cronet_error_code:0 error_msg:network change, cancel all task请求报错

微信小程序请求接口,报request:fail errcode:10011是什么原因,uniapp微信小程序request:fail errcode:10011 cronet_error_code:0 error_msg:network change, cancel all task,微信小程序wx.request常规访问接口,有个别用户的接口请求频繁报错,UniApp 微信小程序uni.request请求接口报错:request:fail errcode:10011 的原因分析与解决方案,苹果ios,安卓

🏆 前端领域优质创作者 4582

[Swift]LeetCode895. 最大频率栈 | Maximum Frequency Stack

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/stren...

weixin_30853329的博客 180

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

leetcode 895. 困难题 Maximum Frequency Stack 最大频率栈

Problem:哈希表记录每种数值的索引列表,以及一个数值列表,pop时查找哈希表,拿到频次最大的索引列表的最大索引,并且update最大索引后面的那些数字的索引,然后从数值列表中删除这个数字,update哈希表。

邹九的个人博客 108

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

LeetCode: 895. Maximum Frequency Stack

LeetCode: 895. Maximum Frequency Stack 题目描述 Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack has two functions: push(int x), which pushes an int...

杨领well的专栏 813

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

leetcode 895. Maximum Frequency Stack

Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStackhas two functions: push(int x), which pushes an integer x onto the stack. pop(), which removes a...

cmy203的博客 182

【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

【LeetCode】895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/maximum-frequency-stack/description/ 题目描述: Implemen...

负雪明烛 1547

Leetcode-895 Maximum Frequency Stack(最大频率堆栈)

1 class FreqStack 2 { 3 public: 4 unordered_map<int,int> hash; 5 vector<int> stk; 6 int max_times; 7 int times_table[10001]; 8 ...

weixin_30410119的博客 208

个人练习-Leetcode-895. Maximum Frequency Stack

leetcode

Rstln的博客 99

LC-895. 最大频率栈(优先队列+哈希表)

阿里20.10;字节21.4;快手22.4;华为22.7难度困难268设计一个类似堆栈的数据结构,将元素推入堆栈,并从堆栈中弹出最高的元素。实现FreqStack构造一个空的堆栈。将一个整数val压入栈顶。int pop()删除并返回堆栈中出现频率最高的元素。

qq_42958831的博客 583

895. Maximum Frequency Stack

Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack has two functions: push(int x), which pushes an integer x onto the stack. pop(), which removes a...

Do not mess it up 408

最大频率栈 Maximum Frequency Stack

2018-10-06 22:01:11 问题描述: 问题求解: 为每个频率创建一个栈即可。 class FreqStack { Map<Integer, Integer> map; List<Stack<Integer>> stacks; public FreqStack() { ...

aitie1479的博客 676

895. 最大频率栈 哈希

895. 最大频率栈,栈+哈希

yu_duan_hun的博客 582
上一篇: leetcode 856. Score of Parentheses(括号的分数)
下一篇: leetcode 1007. Minimum Domino Rotations For Equal Row(行相等的最少多米诺旋转)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址