Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. Rich Cook

Find in Spans

Language Java | Level Intermediate | Category Data structure | August 30, 2015 1:00 pm

Data structure Description

The "stock span" problem is a financial problem where the stock has a series of N daily price quotes for a stock. We need to calculate span of stock’s price for all N days.

Span is the number of consecutive day’s right before that day, which have less or equal stock value. The span of the stock price on a given day defined as the maximum number of consecutive days 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, Input 7 days prices = {100, 80, 60, 70, 60, 75, 85} Span values for corresponding 7 days = {1, 1, 1, 2, 1, 4, 6}
Write a program to find a maximum span of numbers in a given array using stack.



7 days prices of stack:[100, 80, 60, 70, 60, 75, 85]
Print the calculated span values:[1, 1, 1, 2, 1, 4, 6]



No comments available!

Please login to add comments.