#C13482. Maximum Profit Calculation
Maximum Profit Calculation
Maximum Profit Calculation
You are given a list of daily stock prices. Your task is to compute the maximum profit that can be achieved by buying and then later selling the stock. In addition, you must determine the corresponding 0-indexed days on which to buy and sell in order to get this maximum profit.
If no profit is possible, or if there is only a single price, output should be 0 None None
.
More formally, let \(P = [p_0, p_1, \dots, p_{n-1}]\) be the list of prices. You need to find indices \(i\) and \(j\) (with \(0 \leq i < j < n\)) such that the profit \(p_j - p_i\) is maximized. If \(p_j - p_i \leq 0\) for all valid pairs, the answer should be 0 None None
.
inputFormat
The input is given via standard input (stdin) and has the following format:
n p0 p1 ... p(n-1)
Where n
(an integer) denotes the number of days, followed by n
space-separated numbers representing the stock prices for each day.
outputFormat
Output a single line to standard output (stdout) containing three values separated by spaces:
max_profit buy_day sell_day
If no profitable transaction exists, print 0 None None
.
6
7 1 5 3 6 4
5 1 4