-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockSpan.java
40 lines (32 loc) · 1 KB
/
StockSpan.java
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
package Stack;
import java.util.Arrays;
public class StockSpan {
static void calculateSpan(int price[], int n, int S[])
{
// Span value of first day is always 1
S[0] = 1;
// Calculate span value of remaining days by
// linearly checking previous days
for (int i = 1; i < n; i++) {
S[i] = 1; // Initialize span value
// Traverse left while the next element on left
// is smaller than price[i]
for (int j = i - 1;
(j >= 0) && (price[i] >= price[j]); j--)
S[i]++;
}
}
static void printArray(int arr[])
{
System.out.print(Arrays.toString(arr));
}
public static void main(String[] args) {
int price[] = { 10, 4, 5, 90, 120, 80 };
int n = price.length;
int S[] = new int[n];
// Fill the span values in array S[]
calculateSpan(price, n, S);
// print the calculated span values
printArray(S);
}
}