Hi I need some help with this problem Complete the implementation of linear-time algorithm for Stock Span Problem
already I have this code :

for i=0 to n-1 do
 done < false
  while not(D.isEmpty() or done) do
   if P[i]>=P[D.top()] then
     done < true
   if D.isEmpty()then
    h< -1
     h <d.top()
 return array S

Recommended Answers

All 2 Replies

1)I don't know what Stock Span algorithm is. Don't dump the psudo code and ask me to understand it.
2)This is your assignment to implement an algorithm to do it. There is no afford from your side shown here. Please work on it first, not just dump the original question to the forum.
3)It is obvious that your psudo code is not completed. There is supposed to be the portion of initialization (i.e. S, D, etc).


Firstly, I've noticed that the stock span problem has not been clearly defined at many places (including wikipedia). Hence, here is a more easily understandable definition of the problem:

Span of a stock on a given day, is the maximum number of consecutive days just before the given day, for which the price of the stock is less than or equal to the price of stock on the given day.

Since, I'm more comfortable with java, here's the code in java:
(Couldn't paste the code inline due to some false formatting error).

Currently, the definition given in wikipedia is a little ambigious. Link for the wikipedia definition and pseudo code: http://en.wikipedia.org/wiki/Stack_(abstract_data_type)#The_Stock_Span_Problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.