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的更多相关文章

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

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

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

    Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...

  3. LeetCode 895. Maximum Frequency Stack

    题目链接:https://leetcode.com/problems/maximum-frequency-stack/ 题意:实现一种数据结构FreqStack,FreqStack需要实现两个功能: ...

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

    Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...

  5. 最大频率栈 Maximum Frequency Stack

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

  6. Maximum Frequency Stack

    Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...

  7. 【LeetCode】栈 stack(共40题)

    [20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...

  8. [LeetCode] 155. Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  9. Uncaught RangeError: Maximum call stack size exceeded 调试日记

    异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...

随机推荐

  1. 使用VS Code调试Node

    1.双击打开vscode 2.找到底层面板 把ctrl改成LF 2. 3.打开文件夹,建立项目test 4.新建hellow.js 输入: var name='world'; var s='hello ...

  2. Springboot静态文件不更新的解决办法,以及Springboot实现热部署

    Springboot静态文件不更新的解决办法,以及Springboot实现热部署 原文链接:https://www.cnblogs.com/blog5277/p/9271882.html 原文作者:博 ...

  3. JDK下载-安装-配置

    1.JDK的下载 jdk1.8版本   下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-213 ...

  4. 力扣(LeetCode)463. 岛屿的周长

    给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域. 网格中的格子水平和垂直方向相连(对角线方向不相连).整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地 ...

  5. 更改npm淘宝源,并设置cnpm

    一.通过命令配置1. 命令 npm config set registry https://registry.npm.taobao.org2. 验证命令 npm config get registry ...

  6. ionic3使用cnpm可能会出现的问题

    在跑一个ionic3项目的时候发现,新建的页面无法被识别,总是报错 cannot find modules '../pages/login/login.modules' 在排除多种可能性后,确定了是因 ...

  7. 亲自打造Deferred对象

    经过对比之后,决心学习jQuery,自己打造一个Deferred对象.var util = require('./util.js');function Callbacks() { var list = ...

  8. linux下NFS实战

    系统环境 系统平台:CentOS release 6.8 (Final) NFS Server IP:172.16.55.6 防火墙关闭 SELinux=disabled 安装NFS程序包 1.查看系 ...

  9. bzoj 5068: 友好的生物

    大意: n个生物, 每个生物有k种属性, 友好度通过下式计算. , C为给定非负数组, 求友好度最大值. k比较小, 求的是最大值并且$C_i$非负, 所以可以暴力枚举正负情况去绝对值号. #incl ...

  10. JavaScript中的label语句,及应用

    label语句:可以在代码中添加标签,以便将来使用. 语法:label : statement 示例: start: for(var i=0;i<10;i++){ console.log(i); ...