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 950c4f9

Browse files
committed
feat: add solutions to lc problems: No.0121,0122,1014
1 parent 29ee8f9 commit 950c4f9

File tree

23 files changed

+634
-142
lines changed

23 files changed

+634
-142
lines changed

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@
171171
- [环形子数组的最大和](./solution/0900-0999/0918.Maximum%20Sum%20Circular%20Subarray/README.md)
172172
- [乘积最大子序列](./solution/0100-0199/0152.Maximum%20Product%20Subarray/README.md)
173173
- [乘积为正数的最长子数组长度](./solution/1500-1599/1567.Maximum%20Length%20of%20Subarray%20With%20Positive%20Product/README.md)
174+
- [最佳观光组合](./solution/1000-1099/1014.Best%20Sightseeing%20Pair/README.md)
175+
- [买卖股票的最佳时机](./solution/0100-0199/0121.Best%20Time%20to%20Buy%20and%20Sell%20Stock/README.md)
176+
- [买卖股票的最佳时机 II](./solution/0100-0199/0122.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II/README.md)
174177
- [接雨水](./solution/0000-0099/0042.Trapping%20Rain%20Water/README.md)
175178
- [礼物的最大价值](./lcof/面试题47.%20礼物的最大价值/README.md)
176179
- [最小路径和](./solution/0000-0099/0064.Minimum%20Path%20Sum/README.md)

‎README_EN.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
165165
- [Maximum Sum Circular Subarray](./solution/0900-0999/0918.Maximum%20Sum%20Circular%20Subarray/README_EN.md)
166166
- [Maximum Product Subarray](./solution/0100-0199/0152.Maximum%20Product%20Subarray/README_EN.md)
167167
- [Maximum Length of Subarray With Positive Product](./solution/1500-1599/1567.Maximum%20Length%20of%20Subarray%20With%20Positive%20Product/README_EN.md)
168+
- [Best Sightseeing Pair](./solution/1000-1099/1014.Best%20Sightseeing%20Pair/README_EN.md)
169+
- [Best Time to Buy and Sell Stock](./solution/0100-0199/0121.Best%20Time%20to%20Buy%20and%20Sell%20Stock/README_EN.md)
170+
- [Best Time to Buy and Sell Stock II](./solution/0100-0199/0122.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II/README_EN.md)
168171
- [Trapping Rain Water](./solution/0000-0099/0042.Trapping%20Rain%20Water/README_EN.md)
169172
- [Minimum Path Sum](./solution/0000-0099/0064.Minimum%20Path%20Sum/README_EN.md)
170173
- [Decode Ways](./solution/0000-0099/0091.Decode%20Ways/README_EN.md)

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

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,10 @@
5454
```python
5555
class Solution:
5656
def maxProfit(self, prices: List[int]) -> int:
57-
if not prices:
58-
return 0
59-
res = 0
60-
min_price = prices[0]
61-
for price in prices:
62-
min_price = min(min_price, price)
63-
res = max(res, price - min_price)
57+
res, mi = 0, prices[0]
58+
for price in prices[1:]:
59+
res = max(res, price - mi)
60+
mi = min(mi, price)
6461
return res
6562
```
6663

@@ -71,40 +68,93 @@ class Solution:
7168
```java
7269
class Solution {
7370
public int maxProfit(int[] prices) {
74-
if (prices == null) return 0;
75-
int res = 0;
76-
int min = Integer.MAX_VALUE;
77-
for (int price : prices) {
78-
min = Math.min(min, price);
79-
res = Math.max(res, price - min);
71+
int res = 0, mi = prices[0];
72+
for (int i = 1; i < prices.length; ++i) {
73+
res = Math.max(res, prices[i] - mi);
74+
mi = Math.min(mi, prices[i]);
8075
}
8176
return res;
8277
}
8378
}
8479
```
8580

81+
### **C++**
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
int maxProfit(vector<int>& prices) {
87+
int res = 0, mi = prices[0];
88+
for (int i = 1; i < prices.size(); ++i) {
89+
res = max(res, prices[i] - mi);
90+
mi = min(mi, prices[i]);
91+
}
92+
return res;
93+
}
94+
};
95+
```
96+
97+
### **Go**
98+
99+
```go
100+
func maxProfit(prices []int) int {
101+
res, mi := 0, prices[0]
102+
for i := 1; i < len(prices); i++ {
103+
res = max(res, prices[i]-mi)
104+
mi = min(min, prices[i])
105+
}
106+
return res
107+
}
108+
109+
func max(a, b int) int {
110+
if a > b {
111+
return a
112+
}
113+
return b
114+
}
115+
116+
func min(a, b int) int {
117+
if a < b {
118+
return a
119+
}
120+
return b
121+
}
122+
```
123+
86124
### **JavaScript**
87125

88126
```js
89127
/**
90128
* @param {number[]} prices
91129
* @return {number}
92130
*/
93-
const maxProfit = function (prices) {
94-
let min = prices[0];
95-
let profit = 0;
96-
for (let i = 0; i < prices.length; i++) {
97-
if (prices[i] < min) {
98-
min = prices[i];
99-
}
100-
if (profit < prices[i] - min) {
101-
profit = prices[i] - min;
131+
var maxProfit = function(prices) {
132+
let res = 0;
133+
let mi = prices[0];
134+
for (let i = 1; i < prices.length; ++i) {
135+
res = Math.max(res, prices[i] - mi);
136+
mi = Math.min(mi, prices[i]);
102137
}
103-
}
104-
return profit;
138+
return res;
105139
};
106140
```
107141

142+
### **C#**
143+
144+
```cs
145+
public class Solution {
146+
public int MaxProfit(int[] prices) {
147+
int res = 0, mi = prices[0];
148+
for (int i = 1; i < prices.Length; ++i)
149+
{
150+
res = Math.Max(res, prices[i] - mi);
151+
mi = Math.Min(mi, prices[i]);
152+
}
153+
return res;
154+
}
155+
}
156+
```
157+
108158
### **...**
109159

110160
```

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

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,10 @@ Note that buying on day 2 and selling on day 1 is not allowed because you must b
4646
```python
4747
class Solution:
4848
def maxProfit(self, prices: List[int]) -> int:
49-
if not prices:
50-
return 0
51-
res = 0
52-
min_price = prices[0]
53-
for price in prices:
54-
min_price = min(min_price, price)
55-
res = max(res, price - min_price)
49+
res, mi = 0, prices[0]
50+
for price in prices[1:]:
51+
res = max(res, price - mi)
52+
mi = min(mi, price)
5653
return res
5754
```
5855

@@ -61,40 +58,93 @@ class Solution:
6158
```java
6259
class Solution {
6360
public int maxProfit(int[] prices) {
64-
if (prices == null) return 0;
65-
int res = 0;
66-
int min = Integer.MAX_VALUE;
67-
for (int price : prices) {
68-
min = Math.min(min, price);
69-
res = Math.max(res, price - min);
61+
int res = 0, mi = prices[0];
62+
for (int i = 1; i < prices.length; ++i) {
63+
res = Math.max(res, prices[i] - mi);
64+
mi = Math.min(mi, prices[i]);
7065
}
7166
return res;
7267
}
7368
}
7469
```
7570

71+
### **C++**
72+
73+
```cpp
74+
class Solution {
75+
public:
76+
int maxProfit(vector<int>& prices) {
77+
int res = 0, mi = prices[0];
78+
for (int i = 1; i < prices.size(); ++i) {
79+
res = max(res, prices[i] - mi);
80+
mi = min(mi, prices[i]);
81+
}
82+
return res;
83+
}
84+
};
85+
```
86+
87+
### **Go**
88+
89+
```go
90+
func maxProfit(prices []int) int {
91+
res, mi := 0, prices[0]
92+
for i := 1; i < len(prices); i++ {
93+
res = max(res, prices[i]-mi)
94+
mi = min(min, prices[i])
95+
}
96+
return res
97+
}
98+
99+
func max(a, b int) int {
100+
if a > b {
101+
return a
102+
}
103+
return b
104+
}
105+
106+
func min(a, b int) int {
107+
if a < b {
108+
return a
109+
}
110+
return b
111+
}
112+
```
113+
76114
### **JavaScript**
77115

78116
```js
79117
/**
80118
* @param {number[]} prices
81119
* @return {number}
82120
*/
83-
const maxProfit = function (prices) {
84-
let min = prices[0];
85-
let profit = 0;
86-
for (let i = 0; i < prices.length; i++) {
87-
if (prices[i] < min) {
88-
min = prices[i];
89-
}
90-
if (profit < prices[i] - min) {
91-
profit = prices[i] - min;
121+
var maxProfit = function(prices) {
122+
let res = 0;
123+
let mi = prices[0];
124+
for (let i = 1; i < prices.length; ++i) {
125+
res = Math.max(res, prices[i] - mi);
126+
mi = Math.min(mi, prices[i]);
92127
}
93-
}
94-
return profit;
128+
return res;
95129
};
96130
```
97131

132+
### **C#**
133+
134+
```cs
135+
public class Solution {
136+
public int MaxProfit(int[] prices) {
137+
int res = 0, mi = prices[0];
138+
for (int i = 1; i < prices.Length; ++i)
139+
{
140+
res = Math.Max(res, prices[i] - mi);
141+
mi = Math.Min(mi, prices[i]);
142+
}
143+
return res;
144+
}
145+
}
146+
```
147+
98148
### **...**
99149

100150
```
Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
class Solution {
22
public:
33
int maxProfit(vector<int>& prices) {
4-
const int n = prices.size();
5-
if (n < 1) return 0;
6-
vector<int> min_prices(n);
7-
vector<int> max_profit(n);
8-
min_prices[0] = prices[0];
9-
max_profit[0] = 0;
10-
for (int i = 1; i < n; ++i) {
11-
min_prices[i] = min(min_prices[i - 1], prices[i]);
12-
max_profit[i] = max(max_profit[i - 1], prices[i] - min_prices[i - 1]);
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]);
138
}
14-
15-
return max_profit[n - 1];
9+
return res;
1610
}
1711
};
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
public class Solution {
22
public int MaxProfit(int[] prices) {
3-
var result = 0;
4-
var minPrice = int.MaxValue;
5-
foreach (var price in prices)
3+
int res = 0, mi = prices[0];
4+
for (int i = 1; i < prices.Length; ++i)
65
{
7-
if (price > minPrice && result < price - minPrice)
8-
{
9-
result = price - minPrice;
10-
}
11-
if (price < minPrice)
12-
{
13-
minPrice = price;
14-
}
6+
res = Math.Max(res, prices[i] - mi);
7+
mi = Math.Min(mi, prices[i]);
158
}
16-
return result;
9+
return res;
1710
}
1811
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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])
6+
}
7+
return res
8+
}
9+
10+
func max(a, b int) int {
11+
if a > b {
12+
return a
13+
}
14+
return b
15+
}
16+
17+
func min(a, b int) int {
18+
if a < b {
19+
return a
20+
}
21+
return b
22+
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
class Solution {
22
public int maxProfit(int[] prices) {
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);
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]);
97
}
108
return res;
119
}

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22
* @param {number[]} prices
33
* @return {number}
44
*/
5-
const maxProfit = function (prices) {
6-
let min = prices[0];
7-
let profit = 0;
8-
for (let i = 0; i < prices.length; i++) {
9-
if (prices[i] < min) {
10-
min = prices[i];
11-
}
12-
if (profit < prices[i] - min) {
13-
profit = prices[i] - min;
14-
}
5+
var maxProfit = function(prices) {
6+
let res = 0;
7+
let mi = prices[0];
8+
for (let i = 1; i < prices.length; ++i) {
9+
res = Math.max(res, prices[i] - mi);
10+
mi = Math.min(mi, prices[i]);
1511
}
16-
return profit;
17-
};
12+
return res;
13+
};

0 commit comments

Comments
(0)

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