Skip to content

Commit d847680

Browse files
aQuaaQua
aQua
authored and
aQua
committed
122 finish
1 parent a18b740 commit d847680

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package Problem0122
22

33
func maxProfit(prices []int) int {
4+
res := 0
45

6+
for i := 1; i < len(prices); i++ {
7+
if prices[i] > prices[i-1] {
8+
res += prices[i] - prices[i-1]
9+
}
10+
}
11+
12+
return res
513
}

Algorithms/0122.best-time-to-buy-and-sell-stock-ii/best-time-to-buy-and-sell-stock-ii_test.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Problem0122
22

33
import (
4-
"testing"
54
"fmt"
5+
"testing"
66

77
"github.com/stretchr/testify/assert"
88
)
@@ -19,30 +19,47 @@ type para struct {
1919

2020
// ans 是答案
2121
type ans struct {
22-
one int
22+
one int
2323
}
2424

2525
func Test_Problem0122(t *testing.T) {
2626
ast := assert.New(t)
2727

2828
qs := []question{
29+
question{
30+
para{
31+
[]int{7, 2, 1, 5, 3, 6, 4},
32+
},
33+
ans{
34+
7,
35+
},
36+
},
2937

3038
question{
3139
para{
32-
,
40+
[]int{7, 1, 5, 3, 6, 4},
3341
},
3442
ans{
35-
,
43+
7,
3644
},
3745
},
38-
46+
47+
question{
48+
para{
49+
[]int{7, 6, 5, 4, 3, 2, 1},
50+
},
51+
ans{
52+
0,
53+
},
54+
},
55+
3956
// 如需多个测试,可以复制上方元素。
4057
}
4158

4259
for _, q := range qs {
4360
a, p := q.ans, q.para
4461
fmt.Printf("~~%v~~\n", p)
4562

46-
ast.Equal(a.one, maxProfit(p.prices ), "输入:%v", p)
63+
ast.Equal(a.one, maxProfit(p.prices), "输入:%v", p)
4764
}
4865
}

0 commit comments

Comments
 (0)