Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one.
Example 1:
Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.
Example 2:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
Only care about positive profits. Take the frist item as base and scan to
the right. If we encounter an item j
whose price
price[j]
is lower than the base (which means if we sell now
the profit would be negative), we sell j-1
instead and make
j
the new base.
Because price[j]
is lower that the base, using
j
as new base is guaranteed to gain more profit comparing to
the old one.
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
let max = 0
let base = prices[0]
for (let i = 1; i < prices.length; i++) {
const profit = prices[i] - base
if (profit > max) {
max = profit
} else if (profit < 0) {
base = prices[i]
}
}
return max
};
Template generated via Leetmark.