ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 895. Maximum Frequency Stack

2022-08-07 14:03:23  阅读:159  来源: 互联网

标签:max FreqStack pop Stack Frequency freqStack push LeetCode stack


原题链接在这里:https://leetcode.com/problems/maximum-frequency-stack/

题目:

Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.

Implement the FreqStack class:

  • FreqStack() constructs an empty frequency stack.
  • void push(int val) pushes an integer val onto the top of the stack.
  • int pop() removes and returns the most frequent element in the stack.
    • If there is a tie for the most frequent element, the element closest to the stack's top 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
FreqStack freqStack = new FreqStack();
freqStack.push(5); // The stack is [5]
freqStack.push(7); // The stack is [5,7]
freqStack.push(5); // The stack is [5,7,5]
freqStack.push(7); // The stack is [5,7,5,7]
freqStack.push(4); // The stack is [5,7,5,7,4]
freqStack.push(5); // The stack is [5,7,5,7,4,5]
freqStack.pop();   // return 5, as 5 is the most frequent. The stack becomes [5,7,5,7,4].
freqStack.pop();   // return 7, as 5 and 7 is the most frequent, but 7 is closest to the top. The stack becomes [5,7,5,4].
freqStack.pop();   // return 5, as 5 is the most frequent. The stack becomes [5,7,4].
freqStack.pop();   // return 4, as 4, 5 and 7 is the most frequent, but 4 is closest to the top. The stack becomes [5,7].

Constraints:

  • 0 <= val <= 109
  • At most 2 * 104 calls will be made to push and pop.
  • It is guaranteed that there will be at least one element in the stack before calling pop.

题解:

In order to popped the most frequent element and if ther are tie case pop the recent one.

We could have a Map<Integer, Stack<Integer>> group which records the frequency to the stack.

If x are pushed 3 times, then in group(1,2,3), each of them, the stack contains x.

Time Complexity: push, O(1). pop, O(1).

Space: O(n). n is the number of elements in group.

AC Java:

 1 class FreqStack {
 2     Map<Integer, Integer> freq;
 3     Map<Integer, Stack<Integer>> group;
 4     int max;
 5     
 6     public FreqStack() {
 7         freq = new HashMap<>();
 8         group = new HashMap<>();
 9         max = 0;    
10     }
11     
12     public void push(int val) {
13         int f = freq.getOrDefault(val, 0) + 1;
14         freq.put(val, f);
15         group.computeIfAbsent(f, z -> new Stack<Integer>()).push(val);
16         max = Math.max(max, f);
17     }
18     
19     public int pop() {
20         int res = group.get(max).pop();
21         freq.put(res, max - 1);
22         if(group.get(max).isEmpty()){
23             group.remove(max);
24             max--;
25         }
26         
27         return res;
28     }
29 }
30 
31 /**
32  * Your FreqStack object will be instantiated and called as such:
33  * FreqStack obj = new FreqStack();
34  * obj.push(val);
35  * int param_2 = obj.pop();
36  */

 

标签:max,FreqStack,pop,Stack,Frequency,freqStack,push,LeetCode,stack
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/16558947.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有