Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

You are given an array a1...an of stock prices of a commodity on each day, i.e., ai is the price of the stock on day

image text in transcribed

You are given an array a1...an of stock prices of a commodity on each day, i.e., ai is the price of the stock on day i. The span of a stock price on day i is the number of consecutive days before day i for which the price of the stock is less than or equal to ai. You may assume that the price of the stock on day 0 is , so that the span of the stock on day 1 is 0. For example, if the prices were [7,2,4,6,8,5,6], then the span of the stock on day 4 is 2 as there are two consecutive days (days 2 and 3) in which the prices were less than or equal to the price of the stock on day 4 (which is 6). As other examples, the span of the stock on day 5 is 4, and day 7 is 1. Please design and analyze an algorithm that computes the span of the stock on each day. So the output is an array (say b), where bi is the span of the stock on day i. For full credit, your algorithm should run as fast as possible

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students explore these related Databases questions