-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockSpan.java
More file actions
83 lines (67 loc) · 2.49 KB
/
StockSpan.java
File metadata and controls
83 lines (67 loc) · 2.49 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/***************************************************************************************
*
* The stock span problem
*
* The stock span problem is a financial problem where we have a series of n daily
* price quotes for a stock and we need to calculate the span of stock’s price for
* all n days. The span Si of the stock’s price on a given day i is defined as the
* maximum number of consecutive days just before the given day, for which the price
* of the stock on the current day is less than or equal to its price on the given day.
* For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85},
* then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}.
*
*
* Input:
* N = 7, price[] = [100 80 60 70 60 75 85]
*
* Output:
* 1 1 1 2 1 4 6
*
* Explanation:
* Traversing the given input span for 100
* will be 1, 80 is smaller than 100 so the
* span is 1, 60 is smaller than 80 so the
* span is 1, 70 is greater than 60 so the
* span is 2 and so on. Hence the output will
* be 1 1 1 2 1 4 6.
*
*****************************************************************************************/
import java.util.Arrays;
import java.util.Stack;
public class StockSpan {
public static void main(String[] args) {
int price[] = {68, 735, 101, 770, 525, 279, 559}; //1 2 1 4 1 1 3
StockSpan stockSpan = new StockSpan();
int[] res = stockSpan.calculateSpanOptimized(price, price.length);
Arrays.stream(res).forEach(value -> System.out.print(value + " , "));
}
public int[] calculateSpan(int price[], int n) {
int[] result = new int[n];
for (int i = 0; i < n; i++) {
int span = 1;
for (int j = i - 1; j >= 0; j--) {
if (price[j] <= price[i]) {
span++;
} else {
break;
}
}
result[i] = span;
}
return result;
}
public int[] calculateSpanOptimized(int price[], int n) {
int[] result = new int[n];
Stack<Integer> stack = new Stack<>();
stack.push(0);
result[0] = 1;
for (int i = 1; i < n; i++) {
while (!stack.empty() && price[i] >= price[stack.peek()]) {
stack.pop();
}
result[i] = stack.empty() ? i + 1 : i - stack.peek();
stack.push(i);
}
return result;
}
}