-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitignore
54 lines (43 loc) · 1.15 KB
/
.gitignore
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
49
50
51
52
53
54
// TimeComplexity O(N);
// SpaceComplexity O(1);
import java.util.*;
public class StockSpan {
public static int[] StockSpan(int input[]) {
if (input.length == 0){
return input;
}
Stack<Integer> stk = new Stack<>();
stk.push(0);
int ans[] = new int[input.length];
ans[0] = 1;
for(int i = 1 ; i < input.length;i++) {
while(!stk.isEmpty() && input[i] > input[stk.peek()]) {
stk.pop();
}
if(stk.isEmpty()) {
ans[i] = i+1;
}
else {
ans[i]= i-stk.peek();
}
stk.push(i);
}
return ans;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner (System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i<n ;i++) {
arr[i]= sc.nextInt();
}
int []array = StockSpan(arr);
for(int i =0; i<n;i++) {
System.out.print(array[i]+ " ");
}
System.out.println();
}
}
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*