Skip to content

Commit be520ca

Browse files
committed
feat: add solutions to lcof problem: No.63
1 parent 52491cf commit be520ca

File tree

7 files changed

+100
-162
lines changed

7 files changed

+100
-162
lines changed

lcof/面试题63. 股票的最大利润/README.md

+58-96
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,11 @@
3232

3333
## 解法
3434

35-
纯粹的说,此题是在数组当中寻找最大值与最小值,但存在一个限制条件,最大值必须在最小值的后面(相对数组中的存储位置)。
36-
37-
- 暴力解法
38-
- 双指针遍历,记录两数最大差值。
39-
```txt
40-
for i = 0 in arr.length - 1
41-
for j = i in arr.length
42-
r = max(r, arr[j] - arr[i])
43-
```
44-
- 动态规划
45-
- 准备一变量记录最大差值,初始化为 0;一变量记录最小值,初始化为无限大。
46-
- 遍历数组,计算当前遍历元素与最小值的差值,并更新最大差值;再更新最小值。
47-
```txt
48-
r = 0
49-
m = ∞
50-
for i = 0 in arr.length
51-
r = max(r, arr[i] - m)
52-
m = min(m, arr[i])
53-
```
35+
**方法一:动态规划**
36+
37+
我们可以枚举当前的股票价格作为卖出价格,那么买入价格就是在它之前的最低股票价格,此时的利润就是卖出价格减去买入价格。我们可以用一个变量 `mi` 记录之前的最低股票价格,用一个变量 `ans` 记录最大利润,找出最大利润即可。
38+
39+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是数组 `prices` 的长度。
5440

5541
<!-- tabs:start -->
5642

@@ -59,110 +45,89 @@
5945
```python
6046
class Solution:
6147
def maxProfit(self, prices: List[int]) -> int:
62-
if len(prices) == 0:
63-
return 0
64-
mi = prices[0]
65-
res = 0
66-
for val in prices[1:]:
67-
res = max(res, val - mi)
68-
mi = min(mi, val)
69-
return res
48+
mi, ans = inf, 0
49+
for x in prices:
50+
ans = max(ans, x - mi)
51+
mi = min(mi, x)
52+
return ans
7053
```
7154

7255
### **Java**
7356

7457
```java
7558
class Solution {
7659
public int maxProfit(int[] prices) {
77-
int len = prices.length;
78-
if (len == 0) {
79-
return 0;
80-
}
81-
int min = prices[0];
82-
int res = 0;
83-
for (int i = 1; i < len; ++i) {
84-
res = Math.max(res, prices[i] - min);
85-
min = Math.min(min, prices[i]);
60+
int mi = 1 << 30, ans = 0;
61+
for (int x : prices) {
62+
ans = Math.max(ans, x - mi);
63+
mi = Math.min(mi, x);
8664
}
87-
return res;
65+
return ans;
8866
}
8967
}
9068
```
9169

92-
### **JavaScript**
93-
94-
```js
95-
/**
96-
* @param {number[]} prices
97-
* @return {number}
98-
*/
99-
var maxProfit = function (prices) {
100-
let a = 0;
101-
let b = Infinity;
102-
for (let p of prices) {
103-
a = Math.max(a, p - b);
104-
b = Math.min(b, p);
105-
}
106-
return a;
107-
};
108-
```
109-
11070
### **C++**
11171

11272
```cpp
11373
class Solution {
11474
public:
11575
int maxProfit(vector<int>& prices) {
116-
if (prices.size() < 2) {
117-
return 0;
76+
int mi = 1 << 30, ans = 0;
77+
for (int& x : prices) {
78+
ans = max(ans, x - mi);
79+
mi = min(mi, x);
11880
}
119-
120-
int curMin = prices[0];
121-
int maxDiff = prices[1] - prices[0];
122-
123-
for (int i = 2; i < prices.size(); i++) {
124-
if (curMin > prices[i - 1]) {
125-
curMin = prices[i - 1];
126-
}
127-
128-
int diff = prices[i] - curMin;
129-
if (maxDiff < diff) {
130-
maxDiff = diff;
131-
}
132-
}
133-
134-
return maxDiff > 0 ? maxDiff : 0;
81+
return ans;
13582
}
13683
};
13784
```
13885
13986
### **Go**
14087
14188
```go
142-
func maxProfit(prices []int) int {
143-
mi, mx := math.MaxInt32, 0
144-
for _, price := range prices {
145-
mx = max(mx, price-mi)
146-
mi = min(mi, price)
89+
func maxProfit(prices []int) (ans int) {
90+
mi := 1 << 30
91+
for _, x := range prices {
92+
ans = max(ans, x-mi)
93+
mi = min(mi, x)
14794
}
148-
return mx
95+
return
14996
}
15097
151-
func min(x, y int) int {
152-
if x < y {
153-
return x
98+
func max(a, b int) int {
99+
if a > b {
100+
return a
154101
}
155-
return y
102+
return b
156103
}
157104
158-
func max(x, y int) int {
159-
if x > y {
160-
return x
105+
func min(a, b int) int {
106+
if a < b {
107+
return a
161108
}
162-
return y
109+
return b
163110
}
164111
```
165112

113+
### **JavaScript**
114+
115+
```js
116+
/**
117+
* @param {number[]} prices
118+
* @return {number}
119+
*/
120+
var maxProfit = function (prices) {
121+
let mi = 1 << 30;
122+
let ans = 0;
123+
for (const x of prices) {
124+
ans = Math.max(ans, x - mi);
125+
mi = Math.min(mi, x);
126+
}
127+
return ans;
128+
};
129+
```
130+
166131
### **TypeScript**
167132

168133
```ts
@@ -198,16 +163,13 @@ impl Solution {
198163
```cs
199164
public class Solution {
200165
public int MaxProfit(int[] prices) {
201-
if (prices.Length == 0) {
202-
return 0;
203-
}
204-
int mi = prices[0];
205-
int res = 0;
206-
for(int i = 1; i < prices.Length; i++) {
207-
res = Math.Max(res, prices[i] - mi);
208-
mi = Math.Min(mi, prices[i]);
166+
int mi = 1 << 30;
167+
int ans = 0;
168+
foreach(int x in prices) {
169+
ans = Math.Max(ans, x - mi);
170+
mi = Math.Min(mi, x);
209171
}
210-
return res;
172+
return ans;
211173
}
212174
}
213175
```
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
class Solution {
22
public:
33
int maxProfit(vector<int>& prices) {
4-
if (prices.size() < 2) {
5-
return 0;
4+
int mi = 1 << 30, ans = 0;
5+
for (int& x : prices) {
6+
ans = max(ans, x - mi);
7+
mi = min(mi, x);
68
}
7-
8-
int curMin = prices[0];
9-
int maxDiff = prices[1] - prices[0];
10-
11-
for (int i = 2; i < prices.size(); i++) {
12-
if (curMin > prices[i - 1]) {
13-
curMin = prices[i - 1];
14-
}
15-
16-
int diff = prices[i] - curMin;
17-
if (maxDiff < diff) {
18-
maxDiff = diff;
19-
}
20-
}
21-
22-
return maxDiff > 0 ? maxDiff : 0;
9+
return ans;
2310
}
2411
};
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
public class Solution {
22
public int MaxProfit(int[] prices) {
3-
if (prices.Length == 0) {
4-
return 0;
3+
int mi = 1 << 30;
4+
int ans = 0;
5+
foreach(int x in prices) {
6+
ans = Math.Max(ans, x - mi);
7+
mi = Math.Min(mi, x);
58
}
6-
int mi = prices[0];
7-
int res = 0;
8-
for(int i = 1; i < prices.Length; i++) {
9-
res = Math.Max(res, prices[i] - mi);
10-
mi = Math.Min(mi, prices[i]);
11-
}
12-
return res;
9+
return ans;
1310
}
1411
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
func maxProfit(prices []int) int {
2-
mi, mx := math.MaxInt32, 0
3-
for _, price := range prices {
4-
mx = max(mx, price-mi)
5-
mi = min(mi, price)
1+
func maxProfit(prices []int) (ans int) {
2+
mi := 1 << 30
3+
for _, x := range prices {
4+
ans = max(ans, x-mi)
5+
mi = min(mi, x)
66
}
7-
return mx
7+
return
88
}
99

10-
func min(x, y int) int {
11-
if x < y {
12-
return x
10+
func max(a, b int) int {
11+
if a > b {
12+
return a
1313
}
14-
return y
14+
return b
1515
}
1616

17-
func max(x, y int) int {
18-
if x > y {
19-
return x
17+
func min(a, b int) int {
18+
if a < b {
19+
return a
2020
}
21-
return y
22-
}
21+
return b
22+
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
class Solution {
22
public int maxProfit(int[] prices) {
3-
int len = prices.length;
4-
if (len == 0) {
5-
return 0;
3+
int mi = 1 << 30, ans = 0;
4+
for (int x : prices) {
5+
ans = Math.max(ans, x - mi);
6+
mi = Math.min(mi, x);
67
}
7-
int min = prices[0];
8-
int res = 0;
9-
for (int i = 1; i < len; ++i) {
10-
res = Math.max(res, prices[i] - min);
11-
min = Math.min(min, prices[i]);
12-
}
13-
return res;
8+
return ans;
149
}
1510
}

lcof/面试题63. 股票的最大利润/Solution.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
* @return {number}
44
*/
55
var maxProfit = function (prices) {
6-
let a = 0;
7-
let b = Infinity;
8-
for (let p of prices) {
9-
a = Math.max(a, p - b);
10-
b = Math.min(b, p);
6+
let mi = 1 << 30;
7+
let ans = 0;
8+
for (const x of prices) {
9+
ans = Math.max(ans, x - mi);
10+
mi = Math.min(mi, x);
1111
}
12-
return a;
12+
return ans;
1313
};
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
class Solution:
22
def maxProfit(self, prices: List[int]) -> int:
3-
if len(prices) == 0:
4-
return 0
5-
mi = prices[0]
6-
res = 0
7-
for val in prices[1:]:
8-
res = max(res, val - mi)
9-
mi = min(mi, val)
10-
return res
3+
mi, ans = inf, 0
4+
for x in prices:
5+
ans = max(ans, x - mi)
6+
mi = min(mi, x)
7+
return ans

0 commit comments

Comments
 (0)