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 integerx
onto the stack.pop()
, which removes and returns the most frequent element in the stack.- If there is a tie for most frequent element, the element closest to the top of the stack 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:
After making six .push operations, the stack is [5,7,5,7,4,5] from bottom to top. Then: pop() -> returns 5, as 5 is the most frequent.
The stack becomes [5,7,5,7,4]. pop() -> returns 7, as 5 and 7 is the most frequent, but 7 is closest to the top.
The stack becomes [5,7,5,4]. pop() -> returns 5.
The stack becomes [5,7,4]. pop() -> returns 4.
The stack becomes [5,7].
Note:
- Calls to
FreqStack.push(int x)
will be such that0 <= x <= 10^9
. - It is guaranteed that
FreqStack.pop()
won't be called if the stack has zero elements. - The total number of
FreqStack.push
calls will not exceed10000
in a single test case. - The total number of
FreqStack.pop
calls will not exceed10000
in a single test case. - The total number of
FreqStack.push
andFreqStack.pop
calls will not exceed150000
across all test cases.
分析:
因为pop()一定是pop out频率最高,并且位置最靠近stack顶端的,所以,我们可以创建HashMap<Integer, Stack<Integer>> 这样一个map,key是频率,值是同一频率的值。
我们每次push的时候,需要知道那个值当前的频率,所以,我们需要有一个map来保存值与频率的关系。
class FreqStack {
HashMap<Integer, Integer> freq = new HashMap<>();
HashMap<Integer, Stack<Integer>> m = new HashMap<>();
int maxfreq = ; public void push(int x) {
int f = freq.getOrDefault(x, ) + ;
freq.put(x, f);
maxfreq = Math.max(maxfreq, f);
if (!m.containsKey(f)) m.put(f, new Stack<Integer>());
m.get(f).add(x);
} public int pop() {
int x = m.get(maxfreq).pop();
freq.put(x, maxfreq - );
if (m.get(maxfreq).size() == ) maxfreq--;
return x;
}
}
Maximum Frequency Stack的更多相关文章
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- LeetCode - Maximum Frequency Stack
Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...
- [Swift]LeetCode895. 最大频率栈 | Maximum Frequency Stack
Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...
- 最大频率栈 Maximum Frequency Stack
2018-10-06 22:01:11 问题描述: 问题求解: 为每个频率创建一个栈即可. class FreqStack { Map<Integer, Integer> map; Lis ...
- [LeetCode] 895. Maximum Frequency Stack 最大频率栈
Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...
- LeetCode 895. Maximum Frequency Stack
题目链接:https://leetcode.com/problems/maximum-frequency-stack/ 题意:实现一种数据结构FreqStack,FreqStack需要实现两个功能: ...
- Uncaught RangeError: Maximum call stack size exceeded 调试日记
异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...
- Uncaught RangeError: Maximum call stack size exceeded 超出最大调用值(个人解释)
写了段jq后,报这个错,度娘未解,灵光一闪,找到原因,上代码: Html 结构: <a href="javascript:;" class="item-pic&qu ...
- Ext.encode 抛出异常“Uncaught RangeError: Maximum call stack size exceeded”
在用使用Ext.encode(ExtObject)过程中抛出了如下错误: Uncaught RangeError: Maximum call stack size exceeded 实际上,不能用 E ...
随机推荐
- printf:函数参数计算从右向左,从左向右?
造冰箱的大熊猫@cnblogs 2019/8/3 1.问题 某天写了如下代码: unsigned char ReadByteFromFile ( FILE * fp ) { unsigned char ...
- CSP-S 模拟测试57题解
人生第一次A,B层一块考rank2,虽然说分差没几分,但还是值得纪念. 题解: T1 天空龙: 大神题,因为我从不写快读也没有写考场注释的习惯,所以不会做,全hzoi就kx会做,kx真大神级人物. T ...
- Redis evalsha 命令
相当于根据sha1校验码,执行缓存在服务器的一段代码. 这个命令的使用方法类似eval--参数的传入方式等等 使用需要redis版本 >= 2.6.0 语法 *> evalsha sha1 ...
- [笔记]C++声明返回数组指针的函数
数组指针的声明:type (*name)[size]; 由于数组不能拷贝,所以函数不能返回数组.但是函数可以返回指针和引用,所以函数可以返回数组指针或引用. 和数组指针的声明类似: type (*fu ...
- Namenode服务挂
BUG修复:HDFS-13112 这两天排查了小集群Crash的问题,这里先总结下这两天排查的结果 一.查看日志 首先查看了Namenode Crash的时候的日志 (一)以下是patch hdfs- ...
- 最初学习mysql的一些操作留存
一:数据库的初始话操作 mysql -u root -p //数据库的登陆 show databases: //展现数据哭中存储的所有文件 use 数据库名: //进入当前要 ...
- CISCO实验记录一:路由器基本配置
一.路由器基本配置要求 1.设置路由器名为:hehe 2.设置特权模式下password为ccna,secret为ccnp,vty线路密码为ccie 3.所有明文密码都加密 二.路由器基本配置命令 1 ...
- swoole入门简介
原文:https://www.cnblogs.com/dormscript/p/4811921.html 本文主要记录一下学习swoole的过程.填过的坑以及swoole究竟有多么强大! 首先说一下对 ...
- 慎用String.intern()作为synchronized的对象锁
https://www.cnblogs.com/yhlx/p/3498387.html
- Python3+RobotFramewok 用户自定义库的开发(四)
在介绍这个之前,可以先看下python的目录Python\Lib\site-packages下面的文件夹,你会发现这个目录下面有DatabaseLibrary.RequestsLibrary.Sele ...