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 integerxonto 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.pushcalls will not exceed10000in a single test case. - The total number of
FreqStack.popcalls will not exceed10000in a single test case. - The total number of
FreqStack.pushandFreqStack.popcalls will not exceed150000across 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 ...
随机推荐
- Java进阶知识15 Spring的基础配置详解
1.SSH各个的职责 Struts2:是web框架(管理jsp.action.actionform等).Hibernate:是ORM框架,处于持久层.Spring:是一个容器框架,用于配置bean,并 ...
- 【luoguUVA1316】 Supermarket--普通并查集+贪心
题目描述 有一个商店有许多批货,每一批货又有N(0<=N<=10^4 )个商品,同时每一样商品都有收益P_iPi ,和过期时间D_iDi (1<=Pi,,Di <=10^4 ...
- free 内存检测
free打印出的内存信息主要分为两种,一种是安装的内存,一种是用磁盘虚拟的内存,就是这里的Swap,相信装过Linux系统的同学肯定不陌生交换分区,这里Swap大小就是我们分区的时候分配的大小. 下面 ...
- 1.5 JAVA的IO操作
1.5 JAVA的IO操作 参考链接:https://www.runoob.com/java/java-files-io.html 一.JAVA的IO操作 由于JAVA引用外界的数据,或是将自身的数据 ...
- django 快速实现文件上传(四)
继博客(三)实现的, 建两个字段,username 用户存放用户名,headImg 用户存放上传文件的路径. 重新同步数据库: 提示: 这个可能是之前已创建了表中的一条记录,之后模型中增加了一个非空的 ...
- 【软件工程】Alpha冲刺 (5/6)
链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 恩泽(组长) 过去两天完成了哪些任务 描述 学习调用中国天气网API,接近实现天气推送功能 对天气推送的形式进行讨论及重确 ...
- php 设置error_reporting(0)和ini_set('display_errors', 0)之后,还是显示错误
php 5.4 apache 2.2 关闭错误报告和错误显示 依然会显示错误 按照我的理解,error_reporting(0)之后就应该不会显示错误了,这是怎么回事? 后来我又试着在php.ini者 ...
- uni-app 的更新及碰到的问题
uni-app 的更新 我这个是针对 app 的测试,没有考虑 小程序 及 h5,如需考虑请参考 uni-app 的条件编译 当我们将文件打包好之后,我们在手机上就可以下载 apk 文件,安装到我们的 ...
- ESXi 5.1 识别GPT问题
使用paredUtil命令 partedUtil mklabel /dev/disks/eui.227c000155707e0b msdospartedUtil mklabel /dev/disks/ ...
- javascript对象属性和数组的访问
javascript对象属性的访问 假如有对象test:var test = { "a":1, "b":2};直接访问对象test的属性a的值,有两种方法: ...