Back to problems

Maximize stock profit with one or two trades

Algorithm · Uber · Medium

A stock's daily prices are stored in the integer array prices, which is 0-indexed: prices[i] is the price on day i. A trade consists of buying one share on a particular day and selling that share on a later day. You can own at most one share at any moment, so any position must be closed before a new purchase. Part A: One Transaction You may make at most one trade. Return the largest possible profit. If no positive return can be made, return 0. Example 1: Explanation: Buy on…

Checking your access…