niyaznigmatullin

QueueMin

Dec 13th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.42 KB | None | 0 0
  1.     static class QueueMin {
  2.         int[] a;
  3.         int head;
  4.         int tail;
  5.  
  6.         public QueueMin(int n) {
  7.             a = new int[n];
  8.             head = tail = 0;
  9.         }
  10.  
  11.         void add(int x) {
  12.             while (tail > head && a[tail - 1] > x) {
  13.                 --tail;
  14.             }
  15.             a[tail++] = x;
  16.         }
  17.  
  18.         int getMin() {
  19.             if (tail - head == 0) {
  20.                 return Integer.MAX_VALUE;
  21.             }
  22.             return a[head];
  23.         }
  24.  
  25.         void poll(int x) {
  26.             if (a[head] == x) {
  27.                 ++head;
  28.             }
  29.         }
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment