zoukankan      html  css  js  c++  java
  • [LeetCode] 895. 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.

    最大频率栈。

    实现 FreqStack,模拟类似栈的数据结构的操作的一个类。

    FreqStack 有两个函数:

    push(int x),将整数 x 推入栈中。
    pop(),它移除并返回栈中出现最频繁的元素。
    如果最频繁的元素不只一个,则移除并返回最接近栈顶的元素。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/maximum-frequency-stack
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这是一道设计题。题意说的很明白了,我直接给思路。这里我需要

    • 变量max,记录全局最多的出现次数是多少
    • Hashmap<Integer, Integer> freq - 记录每个元素的出现次数
    • HashMap<Integer, Stack<Integer>> map - 记录相同出现次数都有哪些元素,存在stack里面

    当我们做push操作的时候,正常更新freq哈希表中每个不同元素的出现次数;对于map哈希表,我们也将当前push的这个元素放入他对应的出现次数背后的那个stack中。

    当我们做pop操作的时候,正常更新freq哈希表中每个不同元素的出现次数;对于map哈希表,注意因为题目要求pop()函数弹出出现次数最多的元素,所以我们肯定是从map.get(max)这个key背后的stack中弹出元素。注意如果做完pop操作之后这个stack为空了,需要将max--。

    时间O(1)

    空间O(n)

    Java实现

     1 class FreqStack {
     2     // 记录次数
     3     HashMap<Integer, Integer> freq;
     4     // 记录相同出现次数都有哪些元素,用stack储存
     5     HashMap<Integer, Stack<Integer>> map;
     6     int max;
     7 
     8     public FreqStack() {
     9         freq = new HashMap<>();
    10         map = new HashMap<>();
    11         max = 0;
    12     }
    13 
    14     public void push(int x) {
    15         int f = freq.getOrDefault(x, 0) + 1;
    16         freq.put(x, f);
    17         max = Math.max(max, f);
    18         if (!map.containsKey(f)) {
    19             map.put(f, new Stack<Integer>());
    20         }
    21         map.get(f).push(x);
    22     }
    23 
    24     public int pop() {
    25         int x = map.get(max).pop();
    26         freq.put(x, max - 1);
    27         if (map.get(max).size() == 0) {
    28             max--;
    29         }
    30         return x;
    31     }
    32 }
    33 
    34 /**
    35  * Your FreqStack object will be instantiated and called as such:
    36  * FreqStack obj = new FreqStack();
    37  * obj.push(x);
    38  * int param_2 = obj.pop();
    39  */

    LeetCode 题目总结

  • 相关阅读:
    解决“此计算机上已安装了试用版。必须先卸载以前安装的试用版后才能安装另一个试用版。”
    Windows 7 快捷键
    【译】部署Asp.Net MVC 网站到Winodws Azure
    jqGrid专题:搜索
    jqGrid专题:格式化数据
    jqGrid专题:事件文档
    jqGrid专题:方法文档
    jqGrid专题:数据加载
    jqGrid专题:参数文档
    jqGrid专题:jqGrid原理
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14463984.html
Copyright © 2011-2022 走看看