StockPrice Max Profit
shlomibd
427 views
Max Profit!
Suppose we could access yesterday's stock prices as an array, where: The indices are the time in minutes past trade opening time, which was 9:30am local time. The values are the price in dollars of HP stock at that time. So if the stock cost $500 at 10:30am, stockPricesYesterday[60] = 500.
Write an efficient method that takes stockPricesYesterday and returns the best profit I could have made from 1 purchase and 1 sale of 1 HP stock yesterday. For example:
int[] stockPricesYesterday = new int[] {10, 7, 5, 8, 11, 9}; getMaxProfit(stockPricesYesterday); // returns 6 (buying for $5 and selling for $11)
No "shorting"—you must buy before you sell. You may not buy and sell in the same time step (at least 1 minute must pass).
FindMaxProfit
Show Me the Money?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// {
package com.yourself;
import java.util.Arrays;
public class StockPrice {
// }
public static int getMaxProfit(int[] stockPricesYesterday) {
return -1;
}
//{
}
//}
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.