package com.interview.stackqueue;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * Created by saitejatokala on 21/11/15.
 * http://www.geeksforgeeks.org/the-stock-span-problem/
 * Question:
 * The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate span of stock’s price for all n days.
 * The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day.
 * For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}
 *
 * Solution 1:
 * We see that S[i] on day i can be easily computed if we know the closest day preceding i, such that the price is greater than on that day than the price on day i. If such a day exists, let’s call it h(i), otherwise, we define h(i) = -1.
 * The span is now computed as S[i] = i – h(i). See the following diagram.
 */
public class StockSpanProblem {
    public static int[] stockSpan(int[] prices){
        Deque<Integer> stack=new ArrayDeque<>();
        int[] stockSpan=new int[prices.length];
        stockSpan[0]=1;
        stack.push(0);
        for(int i=1;i< prices.length;i++){
            Integer j=stack.peek();
            while(j!=null && prices[j]<prices[i]){
                stack.pop();
                j=stack.peek();
            }
            if(j==null){
                stockSpan[i]=i+1;
            }else{
                stockSpan[i]=i-stack.peek();
            }
            stack.push(i);
        }
        return stockSpan;
    }

    public static void main(String[] args) {
        int[] p={100, 80, 60, 70, 60, 75, 85};
        int[] q=stockSpan(p);
        for (int aQ : q) {
            System.out.println(aQ);
        }
    }
}

@@ -57,16 +57,41 @@
 yDeque;%0A
+import java.util.Arrays;%0A
 import j
@@ -1235,17 +1235,19 @@
 r%3E stack
-=
+ = 
 new Arra
@@ -1281,17 +1281,19 @@
 tockSpan
-=
+ = 
 new int%5B
@@ -1328,17 +1328,19 @@
 kSpan%5B0%5D
-=
+ = 
 1;%0A     
@@ -1348,20 +1348,26 @@
   stack.
-push
+offerFirst
 (0);%0A   
@@ -1378,18 +1378,23 @@
  for
+ 
 (int i
-=1;i
+ = 1; i 
 %3C pr
@@ -1408,13 +1408,16 @@
 ngth
-;
+ ; 
 i++)
+ 
 %7B%0A  
@@ -1430,71 +1430,63 @@
     
-Integer j=stack.peek();%0A            while(j!=null && prices%5Bj%5D%3C
+while (!stack.isEmpty() && prices%5Bstack.peekFirst()%5D %3C 
 pric
@@ -1491,16 +1491,17 @@
 ices%5Bi%5D)
+ 
 %7B%0A      
@@ -1522,41 +1522,15 @@
 k.po
-p();%0A                j=stack.peek
+llFirst
 ();%0A
@@ -1561,17 +1561,27 @@
   if
-(j==null)
+ (stack.isEmpty()) 
 %7B%0A  
@@ -1610,11 +1610,15 @@
 n%5Bi%5D
-=i+
+ = i + 
 1;%0A 
@@ -1633,12 +1633,14 @@
    %7D
+ 
 else
+ 
 %7B%0A  
@@ -1669,11 +1669,15 @@
 n%5Bi%5D
-=i-
+ = i - 
 stac
@@ -1682,16 +1682,21 @@
 ack.peek
+First
 ();%0A    
@@ -1727,12 +1727,18 @@
 ack.
-push
+offerFirst
 (i);
@@ -1841,17 +1841,24 @@
  int%5B%5D p
-=
+rices = 
 %7B100, 80
@@ -1898,10 +1898,17 @@
 t%5B%5D 
-q=
+result = 
 stoc
@@ -1918,83 +1918,66 @@
 an(p
-);%0A        for (int aQ : q) %7B%0A            System.out.println(aQ);%0A        %7D
+rices);%0A        System.out.print(Arrays.toString(result));
 %0A   

update stock span solution