File tree 1 file changed +21
-9
lines changed
1 file changed +21
-9
lines changed Original file line number Diff line number Diff line change 1
1
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
2
2
3
+ var maxProfit = function ( prices ) {
4
+ let max = 0 ;
5
+ let min = Infinity ;
6
+
7
+ for ( let p of prices ) {
8
+ min = Math . min ( min , p ) ;
9
+ max = Math . max ( max , p - min ) ;
10
+ }
11
+
12
+ return max ;
13
+ } ;
14
+
3
15
// p: array
4
16
// num: int +
5
17
// e:
16
28
// Output: 0
17
29
// Explanation: In this case, no transactions are done and the max profit = 0.
18
30
19
- var maxProfit = function ( prices ) {
20
- let maxProfit = 0 ;
21
- let min = Infinity ;
22
- for ( let i = 1 ; i < prices . length ; i ++ ) {
23
- min = Math . min ( min , prices [ i - 1 ] ) ;
24
- maxProfit = Math . max ( maxProfit , prices [ i ] - min ) ;
25
- }
31
+ // var maxProfit = function (prices) {
32
+ // let maxProfit = 0;
33
+ // let min = Infinity;
34
+ // for (let i = 1; i < prices.length; i++) {
35
+ // min = Math.min(min, prices[i - 1]);
36
+ // maxProfit = Math.max(maxProfit, prices[i] - min);
37
+ // }
26
38
27
- return maxProfit ;
28
- } ;
39
+ // return maxProfit;
40
+ // };
29
41
30
42
// var maxProfit = function(prices) {
31
43
// let arr = [0];
You can’t perform that action at this time.
0 commit comments