Skip to content

Commit 27c6356

Browse files
committed
commit solution 121
1 parent f15d84f commit 27c6356

File tree

4 files changed

+100
-5
lines changed

4 files changed

+100
-5
lines changed
Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,62 @@
1-
# [100. xxx](https://leetcode-cn.com/problems/recover-binary-search-tree)
1+
# [121. 买卖股票的最佳时机](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/description/)
22

33
### 题目描述
44

5+
<p>给定一个数组,它的第&nbsp;<em>i</em> 个元素是一支给定股票第 <em>i</em> 天的价格。</p>
6+
7+
<p>如果你最多只允许完成一笔交易(即买入和卖出一支股票一次),设计一个算法来计算你所能获取的最大利润。</p>
8+
9+
<p>注意:你不能在买入股票前卖出股票。</p>
10+
11+
<p>&nbsp;</p>
12+
13+
<p><strong>示例 1:</strong></p>
14+
15+
<pre><strong>输入:</strong> [7,1,5,3,6,4]
16+
<strong>输出:</strong> 5
17+
<strong>解释: </strong>在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
18+
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
19+
</pre>
20+
21+
<p><strong>示例 2:</strong></p>
22+
23+
<pre><strong>输入:</strong> [7,6,4,3,1]
24+
<strong>输出:</strong> 0
25+
<strong>解释: </strong>在这种情况下, 没有交易完成, 所以最大利润为 0。
26+
</pre>
527

628
### 解题思路
729

30+
1. 设定`buy`为 prices[0], `sale`, `max`为0
31+
2. 遍历剩余的数组元素
32+
3. 判断当前数组元素`v`减去`buy`大于0时,表示利润大于0,此时表示可以出售,赋值给`sale`
33+
4. 判断本次出售的`sale`是否大于上一次出售的`max`,如果大于,则更新`max`
34+
5. 判断当前数组元素`v`是否小于`buy`,如果小于,则表示更新`buy`
835

936
### 具体解法
1037

11-
<!-- tabs:start -->
1238

1339
#### **Golang**
1440
```go
15-
41+
func maxProfit(prices []int) int {
42+
if len(prices) == 0 {
43+
return 0
44+
}
45+
buy := prices[0]
46+
sale, max := 0, 0
47+
for _, v := range prices[1:] {
48+
if v-buy > 0 {
49+
sale = v - buy
50+
}
51+
if sale > max {
52+
max = sale
53+
}
54+
if v < buy {
55+
buy = v
56+
}
57+
}
58+
return max
59+
}
1660
```
1761

18-
<!-- tabs:end -->
1962

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=121 lang=golang
5+
*
6+
* [121] 买卖股票的最佳时机
7+
*/
8+
9+
// @lc code=start
10+
func maxProfit(prices []int) int {
11+
if len(prices) == 0 {
12+
return 0
13+
}
14+
buy := prices[0]
15+
sale, max := 0, 0
16+
for _, v := range prices[1:] {
17+
if v-buy > 0 {
18+
sale = v - buy
19+
}
20+
if sale > max {
21+
max = sale
22+
}
23+
if v < buy {
24+
buy = v
25+
}
26+
}
27+
return max
28+
}
29+
30+
// @lc code=end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMaxProfit(t *testing.T) {
8+
var nums []int
9+
var ret int
10+
11+
ret = 8
12+
nums = []int{1, 2, 3, 4, 8, 2, 9}
13+
if ret != maxProfit(nums) {
14+
t.Fatalf("case fails %v\n", ret)
15+
}
16+
17+
ret = 0
18+
nums = []int{}
19+
if ret != maxProfit(nums) {
20+
t.Fatalf("case fails %v\n", ret)
21+
}
22+
}

solution/100-199/_sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
- [118. 杨辉三角 ✅](solution/100-199/0118.pascal%27s-triangle/)
2828
- [119. 杨辉三角 ii](solution/100-199/0119.pascal%27s-triangle-ii/)
2929
- [120. 三角形最小路径和](solution/100-199/0120.triangle/)
30-
- [121. 买卖股票的最佳时机](solution/100-199/0121.best-time-to-buy-and-sell-stock/)
30+
- [121. 买卖股票的最佳时机](solution/100-199/0121.best-time-to-buy-and-sell-stock/)
3131
- [122. 买卖股票的最佳时机 ii](solution/100-199/0122.best-time-to-buy-and-sell-stock-ii/)
3232
- [123. 买卖股票的最佳时机 iii](solution/100-199/0123.best-time-to-buy-and-sell-stock-iii/)
3333
- [124. 二叉树中的最大路径和](solution/100-199/0124.binary-tree-maximum-path-sum/)

0 commit comments

Comments
 (0)