forked from mandliya/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
##Problems solved using stack | ||
1. A simple demo of stack | ||
2. Stock Span Problem | ||
Given : We have series of n daily price quotes for a stock. | ||
Required: We need to calculate **span** of stock's price for all n days. | ||
Span for ith day is defined as **maximum** number of consecutive days, | ||
for which the price of the stock was less than or equal to ith day. | ||
Example: For stock quotes {100, 60, 70, 65, 80, 85} span will be {1, 1, 2, 1, 4, 5}. | ||
Span for day 1 is always 1, now for day 2 stock is at 60, and there is no day befor it when stock was less than 60. | ||
So span remains 1. For day 3, the stock is priced at 70, so its span is 2, as previous day it was 60, and so on. | ||
Approach: | ||
1. First approach is to scan backwards on each day and count the number of days stock was lesser than given day. | ||
As soon we hit a bigger price, we stop. This solution is inefficient and would be O(n^2). | ||
2. Solving it using stack. | ||
1. Span of day1 is always 1. Put it on stack. | ||
2. From day 2, We repeat next steps for all the remaining days | ||
3. If price of the stock on the day on top of stack is less than price of stack on current day, pop it. | ||
4. If price of the stock on the day on top of stack is greater than price of stock on current day, | ||
calculate the span ( span = currentDay - day on top of stack) | ||
5. Push the current day index on the stack. | ||
6. Although it looks like it more than O(n), however each day is pushed on stack once and removed once, | ||
Hence its O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Stock span Problem | ||
* | ||
* Given : We have series of n daily price quotes for a stock. | ||
* | ||
* Required: We need to calculate **span** of stock's price for all n days. | ||
* Span for ith day is defined as **maximum** number of consecutive days, | ||
* for which the price of the stock was less than or equal to ith day. | ||
* | ||
* Example: For stock quotes {100, 60, 70, 65, 80, 85} span will be {1, 1, 2, 1, 4, 5}. | ||
* Span for day 1 is always 1, now for day 2 stock is at 60, and there is no day befor it when stock was less than 60. | ||
* So span remains 1. For day 3, the stock is priced at 70, so its span is 2, as previous day it was 60, and so on. | ||
* Refer readme of for approaches. | ||
*/ | ||
|
||
#include <iostream> | ||
#include <stack.h> | ||
|
||
void stock_spans( int *prices, int *spans, int days ) | ||
{ | ||
algo::Stack<int> st; | ||
|
||
//pushing day one to stack ( days are 0 indexed ) | ||
st.push(0); | ||
|
||
//span for day 1 | ||
spans[0] = 1; | ||
|
||
//span for rest of the days | ||
for ( int i = 1; i < days; ++i ) | ||
{ | ||
// Pop till the day stock price was greater than today and stack is not emptuy. | ||
while ( !st.empty() && prices[i] >= prices[st.top()] ) | ||
{ | ||
st.pop(); | ||
} | ||
|
||
/* | ||
* If stack has emptied, then price[i] is the largest of price we have seen | ||
* so far, else price[i] is greater than price on the day which is on top | ||
* of stack. | ||
*/ | ||
spans[i] = (st.empty()) ? ( i + 1 ) : (i - st.top()) ; | ||
|
||
//push today on stack. | ||
st.push(i); | ||
} | ||
} | ||
|
||
|
||
void printPricesSpans( int *prices, int *spans, int days ) | ||
{ | ||
for ( int i = 0; i < days; ++i ) | ||
{ | ||
std::cout << "Day: " << (i+1) | ||
<< " Price: " << prices[i] | ||
<< " Span: " << spans[i] | ||
<< std::endl; | ||
} | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
int prices[]= { 100, 60, 70, 65, 80, 85 }; | ||
int *spans = new int[6]; | ||
stock_spans(prices, spans, 6); | ||
printPricesSpans( prices, spans, 6 ); | ||
return 0; | ||
} |