Original title: https://oj.leetcode.com/problems/min-stack/solution/
Implements a minimum stack of push (int x), pop (), Top (), Min () methods, which require constant time
This code has been Leetcode accepted, the following analysis results are given by Leetcode:
Hints:
- Consider space-time tradeoff. How would are keep track of the minimums using extra space?
- Make sure to consider duplicate elements.
O(n) Runtime,O(n) Space–extra stack:
Use a extra stack to keep track of the current minimum value. During The push operation we choose the new element or the current minimum, whichever that's smaller to push onto the min Stack.
O(n) Runtime,O(n) Space–minor space optimization:
If a new element is larger than the current minimum, we don't need to push it in to the Min stack. When we perform the pop operation, check if the popped element is the same as the current minimum. If It is, pop it off the min stack too.
Average rating:4.0 (293 votes)
/** * @Author JIANGFQ * */package com.test;import java.util.random;/** * @author JIANGFQ * */public class Minstack { private static final int init_capacity = 16;private static final float FACTOR = 0.75f;private int[] A = null;private int c Urrindex = 0;private int size = 0;private int min = integer.max_value;private int minindex = -1;public Minstack () {a = new Int[init_capacity];} public minstack (int capacity) {if (Capacity < 1 | | capacity > (INTEGER.MAX_VALUE-8)) {throw new Illegalargumentexce Ption ("Illegal capacity value:" + capacity);} A = new int[capacity];} public void push (int x) {if (Size > (integer.max_value-8)) {throw new RuntimeException ("No more space to store VALUE") ;} if (Size > 0 && (size + 1) >= a.length) {int newSize = (int) (size+size*factor); if (NewSize > (integer.max_v ALUE-8) {throw new RuntimeException ("No more space to store value");} Int[] B = new Int[newsize];for (int i = 0; i < size; i++) {b[i] = a[i];} A = b;} if (x < min) {min = X; minindex = size;} a[size++] = x; currindex++; } public void POPs () {if (Currindex < 0) {throw new RuntimeException ("No more value to pop"); } a[currindex--] =-1; size--; SYSTEM.OUT.PRINTLN ("Size:" + size);//System.out.println ("Currindex:" + Currindex + "," + Minindex + "," + A[CU Rrindex]); if (Currindex = = minindex) {int index = 0; int m = A[index]; for (int i = (currIndex-1), I >= 0; i--) {for (int j = i; j >= 0; j--) {//System.out.println ("a=" + M + "," + a[j]); if (M > a[j]) {index = j; m = A[index]; }}} min = A[index]; Minindex = index; } if (size = = 0) {min = Integer.max_value; Minindex =-1; }} public int top () {System.out.println (currindex+ ""); return a[currindex-1]; } public int Getmin () {return min; } public static void Main (String[] args) {Minstack ms = new Minstack ();//Ms.push (2); Ms.push (0); Ms.push (3); Ms.push (0);//System.out.printl N (+ ms.getmin ());//Ms.pop ();//System.out.println (+ ms.getmin ());//Ms.pop ();//System.out.println (+ MS.GETMI n ());//Ms.pop ();//System.out.println (+ ms.getmin ()); Ms.push (2147483646); Ms.push (2147483646); Ms.push (2147483647); System.out.println (Ms.top ()); Ms.pop (); System.out.println (Ms.getmin ()); Ms.pop (); System.out.println (Ms.getmin ()); Ms.pop (); Ms.push (2147483647); System.out.println (Ms.top ()); System.out.println (Ms.getmin ()); Ms.push (-2147483648); System.out.println (Ms.top ()); System.out.println (Ms.getmin ()); Ms.pop (); System.out.println (Ms.getmin ()); } }
Implements a minimum stack of push (int x), pop (), Top (), Min () methods, which require constant time