Skip to content

Commit

Permalink
Day-15 stock span problem
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Aug 31, 2015
1 parent a80c9ca commit 5717346
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
22 changes: 22 additions & 0 deletions stack_problems/README.md
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)
70 changes: 70 additions & 0 deletions stack_problems/stock_span_problem.cpp
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;
}

0 comments on commit 5717346

Please sign in to comment.