LeetCode - 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 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 that 0 <= 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 exceed 10000 in a single test case.
The total number of FreqStack.pop calls will not exceed 10000 in a single test case.
The total number of FreqStack.push and FreqStack.pop calls will not exceed 150000 across all test cases.
Hash map freq
will count the frequence of elements.
Hash map m
is a map of stack.
If element x
has n frequence, we will push x
n times in m[1], m[2] .. m[n]
maxfreq
records the maximum frequence.
push(x)
will push x
tom[++freq[x]]
pop()
will pop from the m[maxfreq]
class FreqStack { HashMap<Integer, Integer> map;
HashMap<Integer, Stack<Integer>> freMap;
int mostFreq; public FreqStack() {
map = new HashMap<>();
freMap = new HashMap<>();
mostFreq = 0;
} public void push(int x) {
int freq = map.getOrDefault(x, 0)+1;
map.put(x, freq);
mostFreq = Math.max(mostFreq, freq);
if(!freMap.containsKey(freq)){
freMap.put(freq, new Stack<Integer>()); }
freMap.get(freq).push(x);
} public int pop() {
int x = freMap.get(mostFreq).pop();
map.put(x, mostFreq-1);
if(freMap.get(mostFreq).size() == 0){
freMap.remove(mostFreq);
mostFreq --;
}
return x;
}
} /**
* Your FreqStack object will be instantiated and called as such:
* FreqStack obj = new FreqStack();
* obj.push(x);
* int param_2 = obj.pop();
*/
LeetCode - Maximum Frequency Stack的更多相关文章
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- [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需要实现两个功能: ...
- [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 ...
- Maximum Frequency Stack
Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...
- 【LeetCode】栈 stack(共40题)
[20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Uncaught RangeError: Maximum call stack size exceeded 调试日记
异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...
随机推荐
- ES6 常用总结(前端开发js技术进阶提升总结)
一.变量声明const和let 在ES6之前,我们都是用var关键字声明变量.无论声明在何处,都会被视为声明在函数的最顶部(不在函数的最顶部就在全局作用域的最顶部).这就是函数变量提升例如: 不用关心 ...
- 无限遍历,Python实现在多维嵌套字典、列表、元组的JSON中获取数据
背景 在做接口自动化的过程中,接口返回的数据是 列表字典循环嵌套 格式的,所以怎样通过一个key值,获取到被包裹了多层的目标数据成为了摆在我面前的一个问题. 一开始没想自己写,但是搜索后发现虽然很 ...
- rabbitMQ 的三种Exchange
rabbitMQ 的Exchange有3种路由方式: direct.fanout.topic ,以下为详细说明 1. Direct Exchange 处理路由键.需要将一个队列绑定到交换机上,要求 ...
- 【转】 ISP概述、工作原理及架构
1.概述 ISP全称Image Signal Processing,即图像信号处理.主要用来对前端图像传感器输出信号处理的单元,以匹配不同厂商的图象传感器. ISP 通过一系列数字图像处理算法完成对数 ...
- LaF: Fast Access to Large ASCII Files
貌似可以随机读取dataframe格式的文本文件.
- Python自学:第二章 使用函数str( )避免类型错误
age = 23 message = "Happy " + str(age) + "rd Birthday" print(message) 输出位 Happy ...
- 『TensorFlow』变量初始化
变量初始化实质 initializer操作的流程是调用Variable节点组中的Assign节点为节点操作单元分配初始值 变量初始化方法 tf.Variable_initializer([variab ...
- 『OpenCV3』滤波器边缘检测
一.原理简介 边缘检测原理 - Sobel, Laplace, Canny算子 X方向Sobel算子 -1 -2 -1 0 0 0 1 2 1 Y方向Sobel算子 -1 0 1 -2 0 2 -1 ...
- c#关于捕获错误的问题
一般捕获错误,采用try,catch,但是有时捕获的错误不明朗,完全不知道是什么错误,这是可以取消try,catch调试运行,找到错误后并修正后,再把try,catch加上.
- C# 用 WebClient 的 Post 方法向 WebServer 传输数据
帮朋友做一个通过Web简单传输数据的例子,百度了一下抄了段代码,完成,效果如下: 其中textBox1里面是客户端需要传输过去的数据,textBox2里面是接收到的返回数据. 代码如下: using ...