Skip to content

Commit f54c7c1

Browse files
committed
feat: add solutions to leetcode problems: No.0121,0122
1 parent 7edf759 commit f54c7c1

File tree

9 files changed

+159
-47
lines changed

9 files changed

+159
-47
lines changed

solution/0100-0199/0121.Best Time to Buy and Sell Stock/README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,35 @@
3737
<!-- 这里可写当前语言的特殊实现逻辑 -->
3838

3939
```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
4150
```
4251

4352
### **Java**
4453

4554
<!-- 这里可写当前语言的特殊实现逻辑 -->
4655

4756
```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+
}
4969
```
5070

5171
### **...**

solution/0100-0199/0121.Best Time to Buy and Sell Stock/README_EN.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,33 @@
4343
### **Python3**
4444

4545
```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
4756
```
4857

4958
### **Java**
5059

5160
```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+
}
5373
```
5474

5575
### **...**
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
class Solution {
22
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);
139
}
14-
return l;
10+
return res;
1511
}
1612
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution:
22
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

solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,61 @@
3838

3939
<!-- 这里可写通用的实现逻辑 -->
4040

41+
所有上涨交易日都做买卖,所有下跌交易日都不做买卖,这样便能实现利润最大化。
42+
4143
<!-- tabs:start -->
4244

4345
### **Python3**
4446

4547
<!-- 这里可写当前语言的特殊实现逻辑 -->
4648

4749
```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
4959
```
5060

5161
### **Java**
5262

5363
<!-- 这里可写当前语言的特殊实现逻辑 -->
5464

5565
```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+
```
5679

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+
};
5796
```
5897
5998
### **...**

solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README_EN.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,50 @@
5757
### **Python3**
5858

5959
```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
6169
```
6270

6371
### **Java**
6472

6573
```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+
```
6687

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+
};
67104
```
68105
69106
### **...**
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
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)
87
{
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);
1210
}
13-
return M ;
11+
return res;
1412
}
1513
};
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
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);
88
}
9-
return maxProfit;
9+
return res;
1010
}
1111
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
class Solution:
22
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

0 commit comments

Comments
 (0)