Skip to content

Commit 2b9d9c7

Browse files
committed
[E:52/531, M:52/969, H:6/386] add No: 121 Best Time to Buy and Sell Stock
1 parent 3903b16 commit 2b9d9c7

File tree

14 files changed

+348
-1
lines changed

14 files changed

+348
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ leetcode 本地化题库,可本地刷题/测试/调试
1717
- 本地化测试用例,本地调试 (cmd 目录)
1818
- 常用刷题 go 语言工具包 (utils 目录)
1919

20+
## 须知
21+
- 抱着怀疑的心态使用测试用例(当然每个测试用例都经过精心测试,难免会有意外)
22+
- 每次执行测试时会先运行一次,检测是否会超时,超时默认为2s
23+
- 不可重复执行的算法,需要注意超时检测这次额外的调用是否影响结果
24+
2025
## 主要功能
2126
### 本地化测试
2227
```shell script
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## [买卖股票的最佳时机](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/)
2+
3+
给定一个数组,它的第 _i_ 个元素是一支给定股票第 _i_ 天的价格。
4+
5+
如果你最多只允许完成一笔交易(即买入和卖出一支股票一次),设计一个算法来计算你所能获取的最大利润。
6+
7+
注意:你不能在买入股票前卖出股票。
8+
9+
 
10+
11+
**示例 1:**
12+
13+
`**输入:** [7,1,5,3,6,4]
14+
**输出:** 5
15+
**解释:** 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
16+
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
17+
`
18+
19+
**示例 2:**
20+
21+
`**输入:** [7,6,4,3,1]
22+
**输出:** 0
23+
**解释:** 在这种情况下, 没有交易完成, 所以最大利润为 0。
24+
`
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/简单/121/golang/solution"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
[7,1,5,3,6,4]
17+
18+
*/
19+
20+
tests := []struct {
21+
name string
22+
input []int
23+
want int
24+
}{
25+
{
26+
name: "test-[7,1,5,3,6,4]",
27+
input: []int{7, 1, 5, 3, 6, 4},
28+
want: 5,
29+
},
30+
{
31+
name: "test-[7,6,4,3,1]",
32+
input: []int{7, 6, 4, 3, 1},
33+
want: 0,
34+
},
35+
}
36+
37+
testLog := leet.NewTestLog(len(tests))
38+
defer testLog.Render()
39+
40+
timeoutDuration := time.Second * 2
41+
42+
for idx, test := range tests {
43+
// 超时检测
44+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
45+
solution.Export(test.input)
46+
cancel()
47+
})
48+
49+
if timeout {
50+
testLog.Fail(idx+1, test.name, "timeout")
51+
continue
52+
}
53+
54+
got := solution.Export(test.input)
55+
if !reflect.DeepEqual(test.want, got) {
56+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
57+
continue
58+
}
59+
60+
testLog.Pass(idx+1, test.name)
61+
}
62+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(prices []int) int {
4+
return maxProfit(prices)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func maxProfit(prices []int) int {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(prices []int) int {
4+
return maxProfit(prices)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func maxProfit(prices []int) int {
13+
14+
}

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## [买卖股票的最佳时机](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/)
2+
3+
给定一个数组,它的第 _i_ 个元素是一支给定股票第 _i_ 天的价格。
4+
5+
如果你最多只允许完成一笔交易(即买入和卖出一支股票一次),设计一个算法来计算你所能获取的最大利润。
6+
7+
注意:你不能在买入股票前卖出股票。
8+
9+
 
10+
11+
**示例 1:**
12+
13+
`**输入:** [7,1,5,3,6,4]
14+
**输出:** 5
15+
**解释:** 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
16+
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
17+
`
18+
19+
**示例 2:**
20+
21+
`**输入:** [7,6,4,3,1]
22+
**输出:** 0
23+
**解释:** 在这种情况下, 没有交易完成, 所以最大利润为 0。
24+
`
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/简单/121/golang/solution"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
[7,1,5,3,6,4]
17+
18+
*/
19+
20+
tests := []struct {
21+
name string
22+
input []int
23+
want int
24+
}{
25+
{
26+
name: "test-[7,1,5,3,6,4]",
27+
input: []int{7, 1, 5, 3, 6, 4},
28+
want: 5,
29+
},
30+
{
31+
name: "test-[7,6,4,3,1]",
32+
input: []int{7, 6, 4, 3, 1},
33+
want: 0,
34+
},
35+
}
36+
37+
testLog := leet.NewTestLog(len(tests))
38+
defer testLog.Render()
39+
40+
timeoutDuration := time.Second * 2
41+
42+
for idx, test := range tests {
43+
// 超时检测
44+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
45+
solution.Export(test.input)
46+
cancel()
47+
})
48+
49+
if timeout {
50+
testLog.Fail(idx+1, test.name, "timeout")
51+
continue
52+
}
53+
54+
got := solution.Export(test.input)
55+
if !reflect.DeepEqual(test.want, got) {
56+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
57+
continue
58+
}
59+
60+
testLog.Pass(idx+1, test.name)
61+
}
62+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(prices []int) int {
4+
return maxProfit(prices)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func maxProfit(prices []int) int {
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(prices []int) int {
4+
return maxProfit(prices)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func maxProfit(prices []int) int {
13+
14+
}

0 commit comments

Comments
 (0)