-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathbest_time_to_buy_and_sell_stock.js
33 lines (29 loc) · 1.25 KB
/
best_time_to_buy_and_sell_stock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Best Time to buy and sell stock
/*
Explanation:
We start by initializing the minimum price to the maximum integer value and the maximum profit to 0.
We loop through the prices array, and for each price:
If the price is less than the current minimum price, we update the minimum price.
Otherwise, if the difference between the price and the minimum price is greater than the current maximum profit, we update the maximum profit.
Finally, we return the maximum profit
Time Complexity: O(n), where n is the length of the prices array.
Space Complexity: O(1), as we are only using two variables to keep track of the minimum price and maximum profit
*/
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function (prices) {
let minPrice = Infinity; // keep track of minimum price seen so far
let maxProfit = 0; // keep track of maximum profit seen so far
for (let i = 0; i < prices.length; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i]; // update minimum price seen so far
} else if (prices[i] - minPrice > maxProfit) {
maxProfit = prices[i] - minPrice; // update maximum profit seen so far
}
}
return maxProfit;
};
const prices = [7, 1, 5, 3, 6, 4];
console.log(maxProfit(prices)); // Output: 5