← Static tasks

716. Max Stack

leetcode hard

#csharp#design#hard#leetcode#search#stack

Task

C# solution

matched/original
public class MaxStack {
    private Stack<int> stack;
    private Stack<int> maxStack;
    public MaxStack() {
        stack = new Stack<int>();
        maxStack = new Stack<int>();
    }
    public void Push(int x) {
        stack.Push(x);
        if (maxStack.Count == 0 || x >= maxStack.Peek()) {
            maxStack.Push(x);
        }
    }
    public int Pop() {
        int x = stack.Pop();
        if (x == maxStack.Peek()) {
            maxStack.Pop();
        }
        return x;
    }
    public int Top() {
        return stack.Peek();
    }
    public int PeekMax() {
        return maxStack.Peek();
    }
    public int PopMax() {
        int maxVal = maxStack.Pop();
        Stack<int> buffer = new Stack<int>();
        while (stack.Peek() != maxVal) {
            buffer.Push(stack.Pop());
        }
        stack.Pop();
        while (buffer.Count > 0) {
            Push(buffer.Pop());
        }
        return maxVal;
    }
}

C++ solution

auto-draft, review before submit
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
public class MaxStack {
    private stack<int> stack;
    private stack<int> maxStack;
    public MaxStack() {
        stack = new stack<int>();
        maxStack = new stack<int>();
    }
    public void Push(int x) {
        stack.push(x);
        if (maxStack.size() == 0 || x >= maxStack.Peek()) {
            maxStack.push(x);
        }
    }
    public int Pop() {
        int x = stack.pop();
        if (x == maxStack.Peek()) {
            maxStack.pop();
        }
        return x;
    }
    public int Top() {
        return stack.Peek();
    }
    public int PeekMax() {
        return maxStack.Peek();
    }
    public int PopMax() {
        int maxVal = maxStack.pop();
        stack<int> buffer = new stack<int>();
        while (stack.Peek() != maxVal) {
            buffer.push(stack.pop());
        }
        stack.pop();
        while (buffer.size() > 0) {
            Push(buffer.pop());
        }
        return maxVal;
    }
}

Java solution

matched/original
import java.util.*;

public class MaxStack {

    private Stack<Integer> stack;
    private Stack<Integer> maxStack;

    public MaxStack() {
        stack = new Stack<>();
        maxStack = new Stack<>();
    }

    public void push(int x) {
        stack.push(x);
        if (maxStack.isEmpty() || x >= maxStack.peek()) {
            maxStack.push(x);
        }
    }

    public int pop() {
        int x = stack.pop();
        if (x == maxStack.peek()) {
            maxStack.pop();
        }
        return x;
    }

    public int top() {
        return stack.peek();
    }

    public int peekMax() {
        return maxStack.peek();
    }

    public int popMax() {
        int maxVal = maxStack.pop();
        Stack<Integer> buffer = new Stack<>();
        while (stack.peek() != maxVal) {
            buffer.push(stack.pop());
        }
        stack.pop();
        while (!buffer.isEmpty()) {
            push(buffer.pop());
        }
        return maxVal;
    }
}

JavaScript solution

matched/original
class MaxStack {
    constructor() {
        this.stack = [];
        this.maxStack = [];
    }

    push(x) {
        this.stack.push(x);
        if (!this.maxStack.length || x >= this.maxStack[this.maxStack.length - 1]) {
            this.maxStack.push(x);
        }
    }

    pop() {
        const x = this.stack.pop();
        if (x === this.maxStack[this.maxStack.length - 1]) {
            this.maxStack.pop();
        }
        return x;
    }

    top() {
        return this.stack[this.stack.length - 1];
    }

    peekMax() {
        return this.maxStack[this.maxStack.length - 1];
    }

    popMax() {
        const maxVal = this.maxStack.pop();
        const buffer = [];
        while (this.stack[this.stack.length - 1] !== maxVal) {
            buffer.push(this.stack.pop());
        }
        this.stack.pop();
        while (buffer.length) {
            this.push(buffer.pop());
        }
        return maxVal;
    }
}

Python solution

matched/original
class MaxStack:

    def __init__(self):
        self.stack = []
        self.max_stack = []

    def push(self, x):
        self.stack.append(x)
        if not self.max_stack or x >= self.max_stack[-1]:
            self.max_stack.append(x)

    def pop(self):
        x = self.stack.pop()
        if x == self.max_stack[-1]:
            self.max_stack.pop()
        return x

    def top(self):
        return self.stack[-1]

    def peekMax(self):
        return self.max_stack[-1]

    def popMax(self):
        max_val = self.max_stack.pop()
        buffer = []
        while self.stack[-1] != max_val:
            buffer.append(self.stack.pop())
        self.stack.pop()
        while buffer:
            self.push(buffer.pop())
        return max_val

Go solution

matched/original
package main

type MaxStack struct {
    stack    []int
    maxStack []int
}

func Constructor() MaxStack {
    return MaxStack{}
}

func (this *MaxStack) Push(x int) {
    this.stack = append(this.stack, x)
    if len(this.maxStack) == 0 || x >= this.maxStack[len(this.maxStack)-1] {
        this.maxStack = append(this.maxStack, x)
    }
}

func (this *MaxStack) Pop() int {
    x := this.stack[len(this.stack)-1]
    this.stack = this.stack[:len(this.stack)-1]
    if x == this.maxStack[len(this.maxStack)-1] {
        this.maxStack = this.maxStack[:len(this.maxStack)-1]
    }
    return x
}

func (this *MaxStack) Top() int {
    return this.stack[len(this.stack)-1]
}

func (this *MaxStack) PeekMax() int {
    return this.maxStack[len(this.maxStack)-1]
}

func (this *MaxStack) PopMax() int {
    maxVal := this.maxStack[len(this.maxStack)-1]
    this.maxStack = this.maxStack[:len(this.maxStack)-1]
    buffer := []int{}
    for this.stack[len(this.stack)-1] != maxVal {
        buffer = append(buffer, this.stack[len(this.stack)-1])
        this.stack = this.stack[:len(this.stack)-1]
    }
    this.stack = this.stack[:len(this.stack)-1]
    for len(buffer) > 0 {
        this.Push(buffer[len(buffer)-1])
        buffer = buffer[:len(buffer)-1]
    }
    return maxVal
}

Explanation