-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockSpanProblem.java
73 lines (65 loc) · 3.15 KB
/
StockSpanProblem.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
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
// Stock span problem
// MediumAccuracy: 43.56%Submissions: 179K+Points: 4
// Be the comment of the day in POTD and win a GfG T-Shirt!
// Solve right now
// banner
// 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 stocks price for all n days.
// The span Si of the stocks 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 given day is less than or equal to its price on the current 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}.
// Example 1:
// 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
// 100 is greater than equal to 100 and there are no more elements behind it so the span is 1,
// 80 is greater than equal to 80 and smaller than 100 so the span is 1,
// 60 is greater than equal to 60 and smaller than 80 so the span is 1,
// 70 is greater than equal to 60,70 and smaller than 80 so the span is 2,
// 60 is greater than equal to 60 and smaller than 70 so the span is 1,
// 75 is greater than equal to 60,70,60,75 and smaller than 100 so the span is 4,
// 85 is greater than equal to 80,60,70,60,75,85 and smaller than 100 so the span is 6.
// Hence the output will be 1 1 1 2 1 4 6.
// Example 2:
// Input:
// N = 6, price[] = [10 4 5 90 120 80]
// Output:
// 1 1 2 4 5 1
// Explanation:
// Traversing the given input span
// 10 is greater than equal to 10 and there are no more elements behind it so the span is 1,
// 4 is greater than equal to 4 and smaller than 10 so the span is 1,
// 5 is greater than equal to 4,5 and smaller than 10 so the span is 2,
// 90 is greater than equal to all previous elements so the span is 4,
// 120 is greater than equal to all previous elements so the span is 5,
// 80 is greater than equal to 80 and smaller than 120 so the span is 1,
// Hence the output will be 1 1 2 4 5 1.
// User Task:
// The task is to complete the function calculateSpan() which takes two parameters, an array price[] denoting the price of stocks, and an integer N denoting the size of the array and number of days. This function finds the span of stock's price for all N days and returns an array of length N denoting the span for the i-th day.
// Expected Time Complexity: O(N).
// Expected Auxiliary Space: O(N).
// Constraints:
// 1 ≤ N ≤ 105
// 1 ≤ C[i] ≤ 105
class Solution {
// Function to calculate the span of stock’s price for all n days.
public static int[] calculateSpan(int stocks[], int n) {
// Your code here
int span[]=new int[stocks.length];
Stack<Integer> st=new Stack<>();
st.push(0);
span[0]=1;
for(int i=1;i<stocks.length;i++){
while(!st.empty() && stocks[st.peek()]<=stocks[i])
st.pop();
if(st.empty()){
span[i]=(i+1);
}else{
span[i]=i-st.peek();
}
st.push(i);
}
return span;
}
}