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 10000in a single test case.
  • The total number of FreqStack.push and FreqStack.pop calls will not exceed 150000 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的更多相关文章

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

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

  2. LeetCode - Maximum Frequency Stack

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

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

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

  4. 最大频率栈 Maximum Frequency Stack

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

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

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

  6. LeetCode 895. Maximum Frequency Stack

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

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

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

  8. Uncaught RangeError: Maximum call stack size exceeded 超出最大调用值(个人解释)

    写了段jq后,报这个错,度娘未解,灵光一闪,找到原因,上代码: Html 结构: <a href="javascript:;" class="item-pic&qu ...

  9. Ext.encode 抛出异常“Uncaught RangeError: Maximum call stack size exceeded”

    在用使用Ext.encode(ExtObject)过程中抛出了如下错误: Uncaught RangeError: Maximum call stack size exceeded 实际上,不能用 E ...

随机推荐

  1. centos6.5linux安装docker之升级内核

    一.运行docker Linux内核版本需要在3.8以上,针对centos6.5 内核为2.6的系统需要先升级内核.不然会特别卡 在yum的ELRepo源中,有mainline(4.5).long-t ...

  2. centos 7配置静态IP,并配置DNS

    注意:四 个文件 cat /etc/sysconfig/network-scripts/ifcfg-eth0 cat /etc/sysconfig/network cat /etc/resolv.co ...

  3. 2017 ICPC乌鲁木齐 A Coins 概率dp

    Coins 题意:一开始所有n个硬币都是反面朝上的,每次必须拿k个来抛,抛的人足够聪明,问m次之后向上的硬币的期望. 首先说了这个足够聪明的意思,就是只要向反面的有k个就不会sb地去拿向正面的来抛,想 ...

  4. Bzoj 1798: [Ahoi2009]Seq 维护序列seq(线段树区间操作)

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小可 ...

  5. [mysql]忘记用户密码或者误删用户账号

    修改你的my.ini或my.cnf文件,在 [mysqld] 节下加入下面一行 skip-grant-tables 然后保存并重启 MySQL 服务. /etc/init.d/mysql restar ...

  6. lyc——2019.10.31

    10:判决素数个数 总时间限制: 1000ms 内存限制: 65536kB 描述 输入两个整数X和Y,输出两者之间的素数个数(包括X和Y). 输入 两个整数X和Y(1 <= X,Y <= ...

  7. CF985C

    CF985C 题意: 你要组成N个木桶,组成每个木桶需要K个木块,(第二行给你N*K个木块),使得任意两个木桶之间的差值不超过L的情况,使得所有木桶可以装的水的和最大,输出这个最大和,如果无法满足要求 ...

  8. Failed to execute goal maven-gpg-plugin 1.5 Sign

    问题描述: 解决办法:跳过maven-gpg-plugin <build> <pluginManagement> <plugins> <plugin> ...

  9. HTML5 烟花系统

    需求:在一个虚拟烟花球中,在不同的部位填充不同颜色.质地的烟花火药:观看试放效果.最好能结合点物理学定律(以便展现火药粉被爆炸扩散到一定程度再爆炸的效果) 这是这学期的一个作业,感觉挺无聊的,纯粹是老 ...

  10. Leetcode题目70.爬楼梯(动态规划+递归-简单)

    题目描述: 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 ...