Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b33f894

Browse files
committed
feat: update solutions to lc problem: No.0121,0122
* No.0121.Best Time to Buy and Sell Stock * No.0122.Best Time to Buy and Sell Stock II
1 parent 29009ae commit b33f894

File tree

17 files changed

+429
-349
lines changed

17 files changed

+429
-349
lines changed

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

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47-
参考本站 [面试题 63. 股票的最大利润](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9863.%20%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E5%A4%A7%E5%88%A9%E6%B6%A6/README.md?id=%E8%A7%A3%E6%B3%95)(题意相同)。
47+
**方法一:枚举 + 维护前缀最小值**
48+
49+
遍历数组 `nums`,对于每个元素 $v,ドル计算其与前面元素的最小值 $mi$ 的差值,取最大值即可。
50+
51+
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
4852

4953
<!-- tabs:start -->
5054

@@ -55,11 +59,11 @@
5559
```python
5660
class Solution:
5761
def maxProfit(self, prices: List[int]) -> int:
58-
res, mi = 0, prices[0]
59-
for price in prices[1:]:
60-
res = max(res, price - mi)
61-
mi = min(mi, price)
62-
return res
62+
ans, mi = 0, inf
63+
for v in prices:
64+
ans = max(ans, v - mi)
65+
mi = min(mi, v)
66+
return ans
6367
```
6468

6569
### **Java**
@@ -69,12 +73,12 @@ class Solution:
6973
```java
7074
class Solution {
7175
public int maxProfit(int[] prices) {
72-
int res = 0, mi = prices[0];
73-
for (int i =1; i <prices.length; ++i) {
74-
res = Math.max(res, prices[i] - mi);
75-
mi = Math.min(mi, prices[i]);
76+
int ans = 0, mi = prices[0];
77+
for (int v :prices) {
78+
ans = Math.max(ans, v - mi);
79+
mi = Math.min(mi, v);
7680
}
77-
return res;
81+
return ans;
7882
}
7983
}
8084
```
@@ -85,26 +89,26 @@ class Solution {
8589
class Solution {
8690
public:
8791
int maxProfit(vector<int>& prices) {
88-
int res = 0, mi = prices[0];
89-
for (int i = 1; i < prices.size(); ++i) {
90-
res = max(res, prices[i] - mi);
91-
mi = min(mi, prices[i]);
92+
int ans = 0, mi = prices[0];
93+
for (int& v : prices) {
94+
ans = max(ans, v - mi);
95+
mi = min(mi, v);
9296
}
93-
return res;
97+
return ans;
9498
}
9599
};
96100
```
97101
98102
### **Go**
99103
100104
```go
101-
func maxProfit(prices []int) int {
102-
res, mi := 0, prices[0]
103-
for i := 1; i < len(prices); i++ {
104-
res = max(res, prices[i]-mi)
105-
mi = min(min, prices[i])
105+
func maxProfit(prices []int) (ans int) {
106+
mi := prices[0]
107+
for _, v := range prices {
108+
ans = max(ans, v-mi)
109+
mi = min(mi, v)
106110
}
107-
return res
111+
return
108112
}
109113
110114
func max(a, b int) int {
@@ -130,13 +134,13 @@ func min(a, b int) int {
130134
* @return {number}
131135
*/
132136
var maxProfit = function (prices) {
133-
let res = 0;
137+
let ans = 0;
134138
let mi = prices[0];
135-
for (let i =1; i <prices.length; ++i) {
136-
res = Math.max(res, prices[i] - mi);
137-
mi = Math.min(mi, prices[i]);
139+
for (constvofprices) {
140+
ans = Math.max(ans, v - mi);
141+
mi = Math.min(mi, v);
138142
}
139-
return res;
143+
return ans;
140144
};
141145
```
142146

@@ -145,13 +149,12 @@ var maxProfit = function (prices) {
145149
```cs
146150
public class Solution {
147151
public int MaxProfit(int[] prices) {
148-
int res = 0, mi = prices[0];
149-
for (int i = 1; i < prices.Length; ++i)
150-
{
151-
res = Math.Max(res, prices[i] - mi);
152-
mi = Math.Min(mi, prices[i]);
152+
int ans = 0, mi = prices[0];
153+
foreach (int v in prices) {
154+
ans = Math.Max(ans, v - mi);
155+
mi = Math.Min(mi, v);
153156
}
154-
return res;
157+
return ans;
155158
}
156159
}
157160
```
@@ -160,13 +163,13 @@ public class Solution {
160163

161164
```ts
162165
function maxProfit(prices: number[]): number {
163-
let res = 0;
164-
let min = Infinity;
165-
for (const price of prices) {
166-
res = Math.max(res, price - min);
167-
min = Math.min(min, price);
166+
let ans = 0;
167+
let mi = prices[0];
168+
for (const v of prices) {
169+
ans = Math.max(ans, v - mi);
170+
mi = Math.min(mi, v);
168171
}
169-
return res;
172+
return ans;
170173
}
171174
```
172175

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

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,24 @@ Note that buying on day 2 and selling on day 1 is not allowed because you must b
4545
```python
4646
class Solution:
4747
def maxProfit(self, prices: List[int]) -> int:
48-
res, mi = 0, prices[0]
49-
for price in prices[1:]:
50-
res = max(res, price - mi)
51-
mi = min(mi, price)
52-
return res
48+
ans, mi = 0, inf
49+
for v in prices:
50+
ans = max(ans, v - mi)
51+
mi = min(mi, v)
52+
return ans
5353
```
5454

5555
### **Java**
5656

5757
```java
5858
class Solution {
5959
public int maxProfit(int[] prices) {
60-
int res = 0, mi = prices[0];
61-
for (int i =1; i <prices.length; ++i) {
62-
res = Math.max(res, prices[i] - mi);
63-
mi = Math.min(mi, prices[i]);
60+
int ans = 0, mi = prices[0];
61+
for (int v :prices) {
62+
ans = Math.max(ans, v - mi);
63+
mi = Math.min(mi, v);
6464
}
65-
return res;
65+
return ans;
6666
}
6767
}
6868
```
@@ -73,26 +73,26 @@ class Solution {
7373
class Solution {
7474
public:
7575
int maxProfit(vector<int>& prices) {
76-
int res = 0, mi = prices[0];
77-
for (int i = 1; i < prices.size(); ++i) {
78-
res = max(res, prices[i] - mi);
79-
mi = min(mi, prices[i]);
76+
int ans = 0, mi = prices[0];
77+
for (int& v : prices) {
78+
ans = max(ans, v - mi);
79+
mi = min(mi, v);
8080
}
81-
return res;
81+
return ans;
8282
}
8383
};
8484
```
8585
8686
### **Go**
8787
8888
```go
89-
func maxProfit(prices []int) int {
90-
res, mi := 0, prices[0]
91-
for i := 1; i < len(prices); i++ {
92-
res = max(res, prices[i]-mi)
93-
mi = min(min, prices[i])
89+
func maxProfit(prices []int) (ans int) {
90+
mi := prices[0]
91+
for _, v := range prices {
92+
ans = max(ans, v-mi)
93+
mi = min(mi, v)
9494
}
95-
return res
95+
return
9696
}
9797
9898
func max(a, b int) int {
@@ -118,13 +118,13 @@ func min(a, b int) int {
118118
* @return {number}
119119
*/
120120
var maxProfit = function (prices) {
121-
let res = 0;
121+
let ans = 0;
122122
let mi = prices[0];
123-
for (let i =1; i <prices.length; ++i) {
124-
res = Math.max(res, prices[i] - mi);
125-
mi = Math.min(mi, prices[i]);
123+
for (constvofprices) {
124+
ans = Math.max(ans, v - mi);
125+
mi = Math.min(mi, v);
126126
}
127-
return res;
127+
return ans;
128128
};
129129
```
130130

@@ -133,13 +133,12 @@ var maxProfit = function (prices) {
133133
```cs
134134
public class Solution {
135135
public int MaxProfit(int[] prices) {
136-
int res = 0, mi = prices[0];
137-
for (int i = 1; i < prices.Length; ++i)
138-
{
139-
res = Math.Max(res, prices[i] - mi);
140-
mi = Math.Min(mi, prices[i]);
136+
int ans = 0, mi = prices[0];
137+
foreach (int v in prices) {
138+
ans = Math.Max(ans, v - mi);
139+
mi = Math.Min(mi, v);
141140
}
142-
return res;
141+
return ans;
143142
}
144143
}
145144
```
@@ -148,13 +147,13 @@ public class Solution {
148147

149148
```ts
150149
function maxProfit(prices: number[]): number {
151-
let res = 0;
152-
let min = Infinity;
153-
for (const price of prices) {
154-
res = Math.max(res, price - min);
155-
min = Math.min(min, price);
150+
let ans = 0;
151+
let mi = prices[0];
152+
for (const v of prices) {
153+
ans = Math.max(ans, v - mi);
154+
mi = Math.min(mi, v);
156155
}
157-
return res;
156+
return ans;
158157
}
159158
```
160159

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public:
33
int maxProfit(vector<int>& prices) {
4-
int res = 0, mi = prices[0];
5-
for (int i = 1; i < prices.size(); ++i) {
6-
res = max(res, prices[i] - mi);
7-
mi = min(mi, prices[i]);
4+
int ans = 0, mi = prices[0];
5+
for (int& v : prices) {
6+
ans = max(ans, v - mi);
7+
mi = min(mi, v);
88
}
9-
return res;
9+
return ans;
1010
}
1111
};
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
public class Solution {
22
public int MaxProfit(int[] prices) {
3-
int res = 0, mi = prices[0];
4-
for (int i = 1; i < prices.Length; ++i)
5-
{
6-
res = Math.Max(res, prices[i] - mi);
7-
mi = Math.Min(mi, prices[i]);
3+
int ans = 0, mi = prices[0];
4+
foreach (int v in prices) {
5+
ans = Math.Max(ans, v - mi);
6+
mi = Math.Min(mi, v);
87
}
9-
return res;
8+
return ans;
109
}
1110
}

‎solution/0100-0199/0121.Best Time to Buy and Sell Stock/Solution.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
func maxProfit(prices []int) int {
2-
res, mi :=0, prices[0]
3-
for i:= 1; i<len(prices); i++ {
4-
res = max(res, prices[i]-mi)
5-
mi = min(min, prices[i])
1+
func maxProfit(prices []int) (ansint) {
2+
mi := prices[0]
3+
for _, v:= rangeprices {
4+
ans = max(ans, v-mi)
5+
mi = min(mi, v)
66
}
7-
returnres
7+
return
88
}
99

1010
func max(a, b int) int {
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution {
22
public int maxProfit(int[] prices) {
3-
int res = 0, mi = prices[0];
4-
for (int i = 1; i < prices.length; ++i) {
5-
res = Math.max(res, prices[i] - mi);
6-
mi = Math.min(mi, prices[i]);
3+
int ans = 0, mi = prices[0];
4+
for (int v : prices) {
5+
ans = Math.max(ans, v - mi);
6+
mi = Math.min(mi, v);
77
}
8-
return res;
8+
return ans;
99
}
1010
}

‎solution/0100-0199/0121.Best Time to Buy and Sell Stock/Solution.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
* @return {number}
44
*/
55
var maxProfit = function (prices) {
6-
let res = 0;
6+
let ans = 0;
77
let mi = prices[0];
8-
for (leti=1;i<prices.length;++i) {
9-
res = Math.max(res,prices[i] - mi);
10-
mi = Math.min(mi, prices[i]);
8+
for (constvofprices) {
9+
ans = Math.max(ans,v - mi);
10+
mi = Math.min(mi, v);
1111
}
12-
return res;
12+
return ans;
1313
};
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def maxProfit(self, prices: List[int]) -> int:
3-
res, mi = 0, prices[0]
4-
for price in prices[1:]:
5-
res = max(res, price - mi)
6-
mi = min(mi, price)
7-
return res
3+
ans, mi = 0, inf
4+
for v in prices:
5+
ans = max(ans, v - mi)
6+
mi = min(mi, v)
7+
return ans
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
function maxProfit(prices: number[]): number {
2-
let res = 0;
3-
let min = Infinity;
4-
for (const price of prices) {
5-
res = Math.max(res,price - min);
6-
min = Math.min(min,price);
2+
let ans = 0;
3+
let mi = prices[0];
4+
for (const v of prices) {
5+
ans = Math.max(ans,v - mi);
6+
mi = Math.min(mi,v);
77
}
8-
return res;
8+
return ans;
99
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /