-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxProfit.js
More file actions
executable file
·37 lines (25 loc) · 1.23 KB
/
maxProfit.js
File metadata and controls
executable file
·37 lines (25 loc) · 1.23 KB
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
34
35
36
37
// Best Time to Buy and Sell Stock - a classic problem that looks simple but teaches powerful concepts!
// Given stock prices over time, find the maximum profit from a single buy-sell transaction.
// Given an array prices where prices[i] is the price of a given stock on the ith day
// have to maximize the profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock
// example input -----> [7, 1, 5, 3, 6, 4] | output --> 5
// why ? buy on day 2 (price lowest) and sell on day 5(price highest)---> profit -> 6 - 1 = 5
// always to buy a the lowest price seen so far, and calculate potential profit at each step
// track the minimum price as we traverse
// at each day calculate profit if we sell today
// keep updating the maximum profit found
// O(n) and space --> O(1)
function maxProfit(prices){
let minPrice = Infinity;
let maxProfit = 0;
for(let price of prices){
// update minimum price
minPrice = Math.min(minPrice, price);
//calculate profit if sold today
let profit = price - minPrice;
// update maximum profit
maxProfit = Math.max(maxProfit, profit);
}
return maxProfit;
}
console.log(maxProfit([7,1,5,3,6,4]));