File tree Expand file tree Collapse file tree 9 files changed +159
-47
lines changed
0121.Best Time to Buy and Sell Stock
0122.Best Time to Buy and Sell Stock II Expand file tree Collapse file tree 9 files changed +159
-47
lines changed Original file line number Diff line number Diff line change 37
37
<!-- 这里可写当前语言的特殊实现逻辑 -->
38
38
39
39
``` python
40
-
40
+ class Solution :
41
+ def maxProfit (self , prices : List[int ]) -> int :
42
+ if not prices:
43
+ return 0
44
+ res = 0
45
+ min_price = prices[0 ]
46
+ for price in prices:
47
+ min_price = min (min_price, price)
48
+ res = max (res, price - min_price)
49
+ return res
41
50
```
42
51
43
52
### ** Java**
44
53
45
54
<!-- 这里可写当前语言的特殊实现逻辑 -->
46
55
47
56
``` java
48
-
57
+ class Solution {
58
+ public int maxProfit (int [] prices ) {
59
+ if (prices == null ) return 0 ;
60
+ int res = 0 ;
61
+ int min = Integer . MAX_VALUE ;
62
+ for (int price : prices) {
63
+ min = Math . min(min, price);
64
+ res = Math . max(res, price - min);
65
+ }
66
+ return res;
67
+ }
68
+ }
49
69
```
50
70
51
71
### ** ...**
Original file line number Diff line number Diff line change 43
43
### ** Python3**
44
44
45
45
``` python
46
-
46
+ class Solution :
47
+ def maxProfit (self , prices : List[int ]) -> int :
48
+ if not prices:
49
+ return 0
50
+ res = 0
51
+ min_price = prices[0 ]
52
+ for price in prices:
53
+ min_price = min (min_price, price)
54
+ res = max (res, price - min_price)
55
+ return res
47
56
```
48
57
49
58
### ** Java**
50
59
51
60
``` java
52
-
61
+ class Solution {
62
+ public int maxProfit (int [] prices ) {
63
+ if (prices == null ) return 0 ;
64
+ int res = 0 ;
65
+ int min = Integer . MAX_VALUE ;
66
+ for (int price : prices) {
67
+ min = Math . min(min, price);
68
+ res = Math . max(res, price - min);
69
+ }
70
+ return res;
71
+ }
72
+ }
53
73
```
54
74
55
75
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int maxProfit (int [] prices ) {
3
- if (prices .length == 0 ) return 0 ;
4
- int m = prices [0 ] , l = 0 ;
5
- for (int i = 1 ; i < prices .length ; i ++) {
6
- int d = prices [i ];
7
- if (d < m ) {
8
- m = d ;
9
- } else {
10
- int n = d - m ;
11
- l = n > l ? n : l ;
12
- }
3
+ if (prices == null ) return 0 ;
4
+ int res = 0 ;
5
+ int min = Integer .MAX_VALUE ;
6
+ for (int price : prices ) {
7
+ min = Math .min (min , price );
8
+ res = Math .max (res , price - min );
13
9
}
14
- return l ;
10
+ return res ;
15
11
}
16
12
}
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def maxProfit (self , prices : List [int ]) -> int :
3
- minprice = ( 1 << 31 ) - 1
4
- maxprofit = 0
5
- for i in range ( len ( prices )):
6
- if prices [i ] <= minprice :
7
- minprice = prices [ i ]
8
- elif prices [ i ] - minprice > maxprofit :
9
- maxprofit = prices [ i ] - minprice
10
- return maxprofit
3
+ if not prices :
4
+ return 0
5
+ res = 0
6
+ min_price = prices [0 ]
7
+ for price in prices :
8
+ min_price = min ( min_price , price )
9
+ res = max ( res , price - min_price )
10
+ return res
Original file line number Diff line number Diff line change 38
38
39
39
<!-- 这里可写通用的实现逻辑 -->
40
40
41
+ 所有上涨交易日都做买卖,所有下跌交易日都不做买卖,这样便能实现利润最大化。
42
+
41
43
<!-- tabs:start -->
42
44
43
45
### ** Python3**
44
46
45
47
<!-- 这里可写当前语言的特殊实现逻辑 -->
46
48
47
49
``` python
48
-
50
+ class Solution :
51
+ def maxProfit (self , prices : List[int ]) -> int :
52
+ if not prices:
53
+ return 0
54
+ res = 0
55
+ for i in range (1 , len (prices)):
56
+ t = prices[i] - prices[i - 1 ]
57
+ res += max (t, 0 )
58
+ return res
49
59
```
50
60
51
61
### ** Java**
52
62
53
63
<!-- 这里可写当前语言的特殊实现逻辑 -->
54
64
55
65
``` java
66
+ class Solution {
67
+ public int maxProfit (int [] prices ) {
68
+ if (prices == null ) return 0 ;
69
+ int res = 0 ;
70
+ for (int i = 1 , n = prices. length; i < n; ++ i) {
71
+ // 策略是所有上涨交易日都做买卖,所以下跌交易日都不做买卖
72
+ int t = prices[i] - prices[i - 1 ];
73
+ res += Math . max(t, 0 );
74
+ }
75
+ return res;
76
+ }
77
+ }
78
+ ```
56
79
80
+ ### ** C++**
81
+
82
+ ``` cpp
83
+ class Solution {
84
+ public:
85
+ int maxProfit(vector<int >& prices) {
86
+ int res = 0, n;
87
+ if ((n = prices.size()) == 0) return 0;
88
+ for (int i = 1; i < n; ++i)
89
+ {
90
+ int t = prices[ i] - prices[ i - 1] ;
91
+ res += max(0, t);
92
+ }
93
+ return res;
94
+ }
95
+ };
57
96
```
58
97
59
98
### **...**
Original file line number Diff line number Diff line change 57
57
### ** Python3**
58
58
59
59
``` python
60
-
60
+ class Solution :
61
+ def maxProfit (self , prices : List[int ]) -> int :
62
+ if not prices:
63
+ return 0
64
+ res = 0
65
+ for i in range (1 , len (prices)):
66
+ t = prices[i] - prices[i - 1 ]
67
+ res += max (t, 0 )
68
+ return res
61
69
```
62
70
63
71
### ** Java**
64
72
65
73
``` java
74
+ class Solution {
75
+ public int maxProfit (int [] prices ) {
76
+ if (prices == null ) return 0 ;
77
+ int res = 0 ;
78
+ for (int i = 1 , n = prices. length; i < n; ++ i) {
79
+ // 策略是所有上涨交易日都做买卖,所以下跌交易日都不做买卖
80
+ int t = prices[i] - prices[i - 1 ];
81
+ res += Math . max(t, 0 );
82
+ }
83
+ return res;
84
+ }
85
+ }
86
+ ```
66
87
88
+ ### ** C++**
89
+
90
+ ``` cpp
91
+ class Solution {
92
+ public:
93
+ int maxProfit(vector<int >& prices) {
94
+ int res = 0, n;
95
+ if ((n = prices.size()) == 0) return 0;
96
+ for (int i = 1; i < n; ++i)
97
+ {
98
+ int t = prices[ i] - prices[ i - 1] ;
99
+ res += max(0, t);
100
+ }
101
+ return res;
102
+ }
103
+ };
67
104
```
68
105
69
106
### **...**
Original file line number Diff line number Diff line change 1
- public:
2
- int maxProfit (vector<int >& prices)
3
- {
4
- if (0 == prices.size ())
5
- return 0 ;
6
- int M = 0 ;
7
- for (int i = 1 ; i < prices.size (); ++i)
1
+ class Solution {
2
+ public:
3
+ int maxProfit (vector<int >& prices) {
4
+ int res = 0 , n;
5
+ if ((n = prices.size ()) == 0 ) return 0 ;
6
+ for (int i = 1 ; i < n; ++i)
8
7
{
9
- int d = prices[i] - prices[i-1 ] ;
10
- if (d > 0 )
11
- M += d ;
8
+ int t = prices[i] - prices[i - 1 ];
9
+ res += max (0 , t);
12
10
}
13
- return M ;
11
+ return res ;
14
12
}
15
13
};
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int maxProfit (int [] prices ) {
3
- int maxProfit = 0 ;
4
- for ( int i = 0 ; i < prices . length ; i ++) {
5
- if ( i + 1 < prices .length && prices [ i + 1 ] - prices [ i ] > 0 ) {
6
- maxProfit + = prices [i + 1 ] - prices [i ];
7
- }
3
+ if ( prices == null ) return 0 ;
4
+ int res = 0 ;
5
+ for ( int i = 1 , n = prices .length ; i < n ; ++ i ) {
6
+ int t = prices [i ] - prices [i - 1 ];
7
+ res += Math . max ( t , 0 );
8
8
}
9
- return maxProfit ;
9
+ return res ;
10
10
}
11
11
}
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def maxProfit (self , prices : List [int ]) -> int :
3
- maxprofit = 0
4
- for i in range (0 , len (prices ) - 1 , 1 ):
5
- if prices [i + 1 ] > prices [i ]:
6
- maxprofit += prices [i + 1 ] - prices [i ]
7
- return maxprofit
3
+ if not prices :
4
+ return 0
5
+ res = 0
6
+ for i in range (1 , len (prices )):
7
+ t = prices [i ] - prices [i - 1 ]
8
+ res += max (t , 0 )
9
+ return res
You can’t perform that action at this time.
0 commit comments