📜  设计一个可以给出最大频率元素的堆栈

📅  最后修改于: 2021-10-27 06:54:37             🧑  作者: Mango

给定N 个元素,任务是实现一个堆栈,该堆栈在每个弹出操作中移除并返回最大频率元素。如果频率存在平局,则将返回最高频率元素。
例子:

方法:维护两个HashMap,一个是频率HashMap,将元素映射到它们的频率,另一个是setMap,它将所有具有相同频率的元素映射到一组(堆栈)中。
FrequencyStack 有两个功能:

  1. push(int x):用频率HashMap映射元素(x)并更新maxfreq变量(即保持最大频率直到现在)。 setMap维护一个堆栈,其中包含具有相同频率的所有元素。
  2. pop():首先从setMap中获取maxfreq元素,然后递减弹出元素的频率。弹出后,如果堆栈变空,则递减 maxfreq。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
using namespace std;
 
// freqMap is to map element to its frequency
map freqMap;
 
// setMap is to map frequency to the
// element list with this frequency
map > setMap;
 
// Variable which stores maximum frequency
// of the stack element
int maxfreq = 0;
 
// Function to insert x in the stack
void push(int x)
{
 
    // Frequency of x
    int freq = freqMap[x] + 1;
 
    // Mapping of x with its frequency
    freqMap[x]= freq;
 
    // Update maxfreq variable
    if (freq > maxfreq)
        maxfreq = freq;
 
    // Map element to its frequency list
    // If given frequency list doesn't exist
    // make a new list of the required frequency
    setMap[freq].push(x);
}
 
// Function to remove maximum frequency element
int pop()
{
 
    // Remove element from setMap
    // from maximum frequency list
    int top = setMap[maxfreq].top();
    setMap[maxfreq].pop();
 
    // Decrement the frequency of the popped element
    freqMap[top]--;
 
    // If whole list is poppped
    // then decrement the maxfreq
    if (setMap[maxfreq].size() == 0)
        maxfreq--;
    return top;
}
 
// Driver code
int main()
{
 
    // Push elements to the stack
    push(4);
    push(6);
    push(7);
    push(6);
    push(8);
 
    // Pop elements
    cout << (pop()) << "\n" ;
    cout << (pop());
    return 0;
}
 
// This code is contributed by Arnab Kundu


Java
// Java implementation of the approach
import java.util.*;
 
public class freqStack {
 
    // freqMap is to map element to its frequency
    static Map freqMap = new HashMap<>();
 
    // setMap is to map frequency to the
    // element list with this frequency
    static Map > setMap = new HashMap<>();
 
    // Variable which stores maximum frequency
    // of the stack element
    static int maxfreq = 0;
 
    // Function to insert x in the stack
    public static void push(int x)
    {
 
        // Frequency of x
        int freq = freqMap.getOrDefault(x, 0) + 1;
 
        // Mapping of x with its frequency
        freqMap.put(x, freq);
 
        // Update maxfreq variable
        if (freq > maxfreq)
            maxfreq = freq;
 
        // Map element to its frequency list
        // If given frequency list doesn't exist
        // make a new list of the required frequency
        setMap.computeIfAbsent(freq, z -> new Stack()).push(x);
    }
 
    // Function to remove maximum frequency element
    public static int pop()
    {
 
        // Remove element from setMap
        // from maximum frequency list
        int top = setMap.get(maxfreq).pop();
 
        // Decrement the frequency of the popped element
        freqMap.put(top, freqMap.get(top) - 1);
 
        // If whole list is poppped
        // then decrement the maxfreq
        if (setMap.get(maxfreq).size() == 0)
            maxfreq--;
        return top;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Push elements to the stack
        push(4);
        push(6);
        push(7);
        push(6);
        push(8);
 
        // Pop elements
        System.out.println(pop());
        System.out.println(pop());
    }
}


Python3
# Python3 implementation of the approach
 
# freqMap is to map element to its frequency
freqMap = {};
 
# setMap is to map frequency to the
# element list with this frequency
setMap = {};
 
# Variable which stores maximum frequency
# of the stack element
maxfreq = 0;
 
# Function to insert x in the stack
def push(x) :
    global maxfreq;
    if x not in freqMap :
        freqMap[x] = 0
         
    # Frequency of x
    freq = freqMap[x] + 1;
     
    # Mapping of x with its Frequency
    freqMap[x]= freq
     
    # Update maxfreq Variable
    if (freq > maxfreq) :
        maxfreq = freq
         
    # Map element to its frequency list
    # If given frequency list doesn't exist
    # make a new list of the required frequency
    if freq not in setMap :
        setMap[freq] = []
     
    setMap[freq].append(x);
 
# Function to remove maximum frequency element
def pop() :
 
    global maxfreq
     
    # Remove element from setMap
    # from maximum frequency list
    top = setMap[maxfreq][-1];
    setMap[maxfreq].pop();
     
    # Decrement the frequency
    # of the popped element
    freqMap[top] -= 1;
     
    # If whole list is poppped
    # then decrement the maxfreq
    if (len(setMap[maxfreq]) == 0) :
        maxfreq -= 1;
         
    return top;
 
# Driver code
if __name__ == "__main__" :
 
    # Push elements to the stack
    push(4);
    push(6);
    push(7);
    push(6);
    push(8);
 
    # Pop elements
    print(pop()) ;
    print(pop());
 
# This code is contributed by AnkitRai01


Javascript


输出:
6
8

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程