-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleetcode-121-BestTimeToBuyAndSellStock.js
118 lines (98 loc) · 2.79 KB
/
leetcode-121-BestTimeToBuyAndSellStock.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
var maxProfit = function (prices) {
let max = 0;
let min = Infinity;
for (let p of prices) {
min = Math.min(min, p);
max = Math.max(max, p - min);
}
return max;
};
// p: array
// num: int +
// e:
// Example 1:
// Input: prices = [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.
// Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
// Example 2:
// Input: prices = [7,6,4,3,1]
// Output: 0
// Explanation: In this case, no transactions are done and the max profit = 0.
// var maxProfit = function (prices) {
// let maxProfit = 0;
// let min = Infinity;
// for (let i = 1; i < prices.length; i++) {
// min = Math.min(min, prices[i - 1]);
// maxProfit = Math.max(maxProfit, prices[i] - min);
// }
// return maxProfit;
// };
// var maxProfit = function(prices) {
// let arr = [0];
// let min = Infinity;
// for (let i = 1; i < prices.length; i++) {
// min = Math.min(min, prices[i - 1])
// arr[i] = prices[i] - min;
// }
// return Math.max(...arr);
// };
// Constraints:
// 1 <= prices.length <= 105
// 0 <= prices[i] <= 104
// var maxProfit = function (prices) {
// let leftPrice = prices[0];
// let rightPrice = prices[1];
// let profit = 0;
// for (let i = 0; i < prices.length; ++i) {
// console.log(leftPrice, rightPrice, profit);
// if (leftPrice < rightPrice) {
// profit = Math.max(profit, rightPrice - leftPrice);
// } else {
// leftPrice = rightPrice;
// }
// rightPrice = prices[i + 1];
// }
// console.log(profit);
// return profit;
// };
// maxProfit([7, 1, 5, 3, 6, 4]);
// maxProfit([7, 6, 4, 3, 1]);
// maxProfit([7, 1, 9, 0, 5]);
// maxProfit([1, 2]);
maxProfit([1, 2, 4]);
// 7, 1, 9, 0, 5
// 7
// S B
// 7 9
// 7: 1-7 5-7 3-7 6-7 4-7
// 1: 5-1 3-1 6-1 4-1
// 5: 3-5 6-5 4-5
// 3: 6 4
// 6: 4
// 4: 4
// 7 1 5 3 6 4
// 7:
// 1:
// 5:
// 3:
// 6:
// 4:
// ///////////////////////////////////// time limit exceeded
// var maxProfit = function (prices) {
// // make 2 for loops and pick the max
// const result = [];
// for (let i = 0; i < prices.length; ++i) {
// let priceDifference = [];
// for (let j = i + 1; j < prices.length; ++j) {
// priceDifference.push(prices[j] - prices[i]);
// }
// console.log(priceDifference);
// result.push(Math.max(...priceDifference));
// }
// console.log(Math.max(...result) > -1 ? Math.max(...result) : 0);
// return Math.max(...result) > -1 ? Math.max(...result) : 0;
// };
// maxProfit([7, 1, 5, 3, 6, 4]);
// maxProfit([2, 1, 4]); //3