|
32 | 32 |
|
33 | 33 | ## 解法
|
34 | 34 |
|
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` 的长度。 |
54 | 40 |
|
55 | 41 | <!-- tabs:start -->
|
56 | 42 |
|
|
59 | 45 | ```python
|
60 | 46 | class Solution:
|
61 | 47 | 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 |
70 | 53 | ```
|
71 | 54 |
|
72 | 55 | ### **Java**
|
73 | 56 |
|
74 | 57 | ```java
|
75 | 58 | class Solution {
|
76 | 59 | 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); |
86 | 64 | }
|
87 |
| - return res; |
| 65 | + return ans; |
88 | 66 | }
|
89 | 67 | }
|
90 | 68 | ```
|
91 | 69 |
|
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 |
| - |
110 | 70 | ### **C++**
|
111 | 71 |
|
112 | 72 | ```cpp
|
113 | 73 | class Solution {
|
114 | 74 | public:
|
115 | 75 | 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); |
118 | 80 | }
|
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; |
135 | 82 | }
|
136 | 83 | };
|
137 | 84 | ```
|
138 | 85 |
|
139 | 86 | ### **Go**
|
140 | 87 |
|
141 | 88 | ```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) |
147 | 94 | }
|
148 |
| - return mx |
| 95 | + return |
149 | 96 | }
|
150 | 97 |
|
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 |
154 | 101 | }
|
155 |
| - return y |
| 102 | + return b |
156 | 103 | }
|
157 | 104 |
|
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 |
161 | 108 | }
|
162 |
| - return y |
| 109 | + return b |
163 | 110 | }
|
164 | 111 | ```
|
165 | 112 |
|
| 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 | + |
166 | 131 | ### **TypeScript**
|
167 | 132 |
|
168 | 133 | ```ts
|
@@ -198,16 +163,13 @@ impl Solution {
|
198 | 163 | ```cs
|
199 | 164 | public class Solution {
|
200 | 165 | 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); |
209 | 171 | }
|
210 |
| - return res; |
| 172 | + return ans; |
211 | 173 | }
|
212 | 174 | }
|
213 | 175 | ```
|
|
0 commit comments