LeetCode 895. Maximum Frequency Stack
2022/8/7 23:23:48
本文主要是介绍LeetCode 895. Maximum Frequency 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 integerval
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 topush
andpop
. - 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 */
这篇关于LeetCode 895. Maximum Frequency Stack的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升