/**
 * Company
 * Copyright (C) 2014-2017 All Rights Reserved.
 */
package com.cwenao.trying.stackandqueue;

/**
 * @author cwenao
 * @version $Id StockSpanByStack.java, v 0.1 2017-09-29 12:50 cwenao Exp $$
 */
public class StockSpanByStack {
}

@@ -102,16 +102,41 @@
 queue;%0A%0A
+import java.util.Stack;%0A%0A
 /**%0A * @
@@ -261,10 +261,719 @@
 Stack %7B%0A
+    static void calculateSpan(int%5B%5D price, int%5B%5D result, int n) %7B%0A%0A        Stack%3CInteger%3E stack = new Stack%3C%3E();%0A        stack.push(0);%0A%0A        result%5B0%5D = 1;%0A        for (int i = 1; i %3C n; i++) %7B%0A            while (!stack.isEmpty() && price%5Bstack.peek()%5D %3C= price%5Bi%5D) %7B%0A                stack.pop();%0A            %7D%0A            result%5Bi%5D = stack.isEmpty() ? i + 1 : i - stack.peek();%0A            stack.push(i);%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        int%5B%5D price = %7B10, 4, 5, 90, 120, 80%7D;%0A        int n = price.length;%0A        int%5B%5D result = new int%5Bn%5D;%0A%0A        calculateSpan(price, result, n);%0A%0A        for (int x : result) %7B%0A            System.out.println(x);%0A        %7D%0A    %7D%0A
 %7D%0A

update the stock span by stack