-
Notifications
You must be signed in to change notification settings - Fork 0
/
profit-algo.js
44 lines (36 loc) · 1.08 KB
/
profit-algo.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
34
35
36
37
38
39
40
41
42
43
//code from https://www.geeksforgeeks.org/stock-buy-sell/
function maxProfit(prices, N) {
let n = N;
let cost = 0;
let maxCost = 0;
if (n == 0) {
return 0;
}
// Store the first element of array
// // in a variable
// let min_price = prices[0];
let mini = Math.min(...prices)
for (let i = 0; i < n; i++) {
// Now compare first element with all
// the element of array and find the
// minimum element
// min_price = Math.min(min_price, prices[i]);
// Since min_price is smallest element of the
// array so subtract with every element of the
// array and return the maxCost
cost = prices[i] - mini;
// console.log('Cost ', mini, cost)
maxCost = Math.max(maxCost, cost);
// console.log(`Max Cost ${maxCost}`)
}
return maxCost;
}
// Driver Code
// Stock prices on consecutive days
let prices = [7, 1, 5, 3, 6, 4];
let assessmentAlgo = [6, 0, -1, 10];
let N = prices.length;
let A = assessmentAlgo.length;
// console.log(maxProfit(prices, N));
console.log(assessmentAlgo);
console.log(maxProfit(assessmentAlgo, assessmentAlgo.length));