-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStockSpan.java
More file actions
48 lines (42 loc) · 967 Bytes
/
StockSpan.java
File metadata and controls
48 lines (42 loc) · 967 Bytes
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
37
38
39
40
41
42
43
44
45
46
47
48
package lecture9a14;
import java.util.Scanner;
import lecture9a13.Stack;
public class StockSpan {
static Scanner a= new Scanner(System.in);
public static void main(String[] args) throws Exception {
int[] arr = takeinput();
int[] ans = new int[arr.length];
ans = stockSpan(arr, ans);
int i=0 ;
for(int val : ans) {
System.out.print( val+" " );
i++;
}
System.out.print("END");
}
public static int[] stockSpan(int[] arr, int[] ans) throws Exception {
Stack s = new Stack(100);
for (int i = 0; i < arr.length; i++) {
while (!s.isEmpty() && arr[s.peek()] < arr[i]) {
s.pop();
}
if(s.isEmpty()) {
ans[i]=i+1;
}else {
ans[i]=i-s.peek();
}
s.push(i);
}
return ans;
}
public static int[] takeinput() {
int[] array;
System.out.println(" enter size ");
int n = a.nextInt();
array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = a.nextInt();
}
return array;
}
}