Datasets:

ArXiv:
License:
File size: 628 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.*;
import java.io.*;
public class StockSpan{

  static void solve(int a[], int n){

    int span[] = new int[n];
    Stack<Integer> st = new Stack<>();
    st.push(0);

    for(int i = 1; i <n; i++){

      while(st.size() > 0 && a[i] > a[st.peek()]){
        st.pop();
      }

      if(st.size()==0){
        span[i] = i+1;
      }
      else{
        span[i] = i-st.peek();
      }
      st.push(i);
    }

    for(int i = 1; i <n; i++){
      System.out.print(span[i]+" ");
    }
  }
  public static void main(String[] args) {
   int a[] = {100,80,60,70,60,75,85};
   int n = a.length;
   solve(a,n);
  }
}