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. 在WPF中调用文件夹浏览/选择对话框

    var dialog = new System.Windows.Forms.FolderBrowserDialog(); System.Windows.Forms.DialogResult resul ...

  2. Qt获取选择的文件夹和文件路径

    获取文件夹路径 static QString getExistingDirectory(QWidget *parent = Q_NULLPTR, const QString &caption ...

  3. css js 兼容问题

    js  兼容问题 1. document.form.item 问题问题:代码中存在 document.formName.item("itemName") 这样的语句,不能在FF下运 ...

  4. LINUX介绍

    Linux操作系统被称为领先的服务器操作系统之一,它被普遍和广泛使用着.全球大约有数百款的Linux系统版本,每个系统版本都有自己的特性和目标人群. Linux的发行版本可以大体分为两类,一类是商业公 ...

  5. 浅谈中大型企业CMDB的建设

    作者:嘉维蓝鲸产品总监,贺勇 针对CMDB这个主题,之前一直想写一篇文章来表达我的看法,但是之前一直不敢写,为什么?因为CMDB这个主题属于一提大家都懂,但是深入讨论大家都晕菜的一个话题:在2018年 ...

  6. jvm是如何管理内存的 .ZT

    http://blog.csdn.net/u014421556/article/details/51744044

  7. 查看值是否传过来php

    <input value='{{mid}}'></input> <input value='{{share}}'></input>

  8. Java-Mail邮件开发

    Email的历史比Web还要久远,直到现在,Email也是互联网上应用非常广泛的服务. 几乎所有的编程语言都支持发送和接收电子邮件,但是,先等等,在我们开始编写代码之前,有必要搞清楚电子邮件是如何在互 ...

  9. spring boot多模块项目找不到类

    项目结构 mapper依赖pojo, service依赖mapper和pojo portal依赖pojo和service. 全都是maven模块 <groupId>com.haitian& ...

  10. jQuery.ready() 函数详解

    jQuery.ready() 函数详解 ready()函数用于在当前文档结构载入完毕后立即执行指定的函数. 该函数的作用相当于window.onload事件. 你可以多次调用该函数,从而绑定多个函数, ...