Skip to content
Rain Hu's Workspace
Go back

[Leetcode] Maximum Frequency Stack 最大頻率堆疊

Rain Hu

題目

題目描述

題目範例

輸入
[“FreqStack”, “push”, “push”, “push”, “push”, “push”, “push”, “pop”, “pop”, “pop”, “pop”]
[[], [5], [7], [5], [7], [4], [5], [], [], [], []]
輸出
[null, null, null, null, null, null, null, 5, 7, 5, 4]

說明

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].

解題

想法

FreqStack freqStack = new FreqStack();
freqStack.push(5); // [5]
freqStack.push(7); // [5,7]
freqStack.push(5); // [5,7,5]
freqStack.push(7); // [5,7,5,7]
freqStack.push(4); // [5,7,5,7,4]
// 此時 4 因為並非最頻繁的元素,所以要將 4 往下推,即變成 [5,7,4,5,7]
freqStack.push(5); // [5,7,4,5,7,5]
freqStack.pop();   // return 5, [5,7,4,5,7]
freqStack.pop();   // return 7, [5,7,4,5]
freqStack.pop();   // return 5, [5,7,4]
freqStack.pop();   // return 4, [5,7]
freqStack[0] = [5,7,4] // 檢查元素是否出現在 freqStack[0] 否則則往freqStack[1] 移動
freqStack[1] = [5,7]
freqStack[2] = [5]  // pop 的時候,從freqStack[2] 開始取,空了則將 freqStack[2] 移除

實作1: List of Stacks

public class freqStack{
    // Field
    List<Stack<Integer>> stacks;

    // Constructor
    public freqStack(){
        stacks = new ArrayList<>();
    }

    // Methods
    public void push(int val){
        push(val, 0);
    }

    private void push(int val, int freq){
        // 當 stacks[freq] 是空的時候,則新建一個 stack。
        Stack<Integer> stack;
        if (freq >= stacks.size()){
            stack = new Stack<>();
            stacks.add(stack);
        } else {
            stack = stacks.get(freq);
        }
        // 當該 stacks[freq] 已經有該元素,則往下一個 stacks 找
        if (stack.contains(val)){
            push(val, freq + 1);
        } else {
            stack.push(val);
        }
    }

    public int pop(){
        // 直接找到最高的 stack,然後把頂端的元素 pop 出。
        Stack<Integer> stack = stacks.get(stacks.size() - 1);
        int top = stack.pop();
        if (stack.isEmpty()){
            stacks.remove(stacks.size() - 1);
        }
        return top;
    }
}

實作2: Use HashMap to record freqency

public class freqStack{
    // Field
    List<Stack<Integer>> stacks;
    Map<Integer, Integer> map; // 用來記錄出現次數

    // Constructor
    public freqStack(){
        stacks = new ArrayList<>();
        map = new HashMap();
    }

    // Methods
    public void push(int val){
        Stack<Integer> stack;
        // 還沒有此出現次數的元素出現,則新增此 stack
        if (stacks.size() < map.getOrDefault(val, 0) + 1){
            stack = new Stack<>();
        } else {
            // 取得此元素出現的次數,若沒出現過則取得 stacks[0]
            stack = stacks.get(map.getOrDefault(val, 0));
        }
        stack.push(val);
        map.put(val, map.getOrDefault(val, 0) + 1); // 更新出現次數
    }

    public int pop(){
        // 直接找到最高的 stack,然後把頂端的元素 pop 出。
        Stack<Integer> stack = stacks.get(stacks.size() - 1);
        int top = stack.pop();
        map.put(val, map.get(val) - 1); // 更新出現次數
        if (stack.isEmpty()){
            stacks.remove(stacks.size() - 1);
        }
        return top;
    }
}

程式碼

Reference: Leetcode: 895. Maximum Frequency Stack


Share this post on:

Previous
[CA] 計算機的抽象化與科技
Next
[Java] 面試常見問題