8000 Merge pull request #155 from kylesliu/develop · reverse/awesome-golang-leetcode@c57e508 · GitHub
[go: up one dir, main page]

Skip to content

Commit c57e508

Browse files
author
Kyle Liu
authored
Merge pull request 6boris#155 from kylesliu/develop
add 309 solution
2 parents eb879d1 + 888f681 commit c57e508

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func maxProfit(prices []int) int {
4+
sold, held, reset := -1<<31, -1<<31, 0
5+
6+
for _, price := range prices {
7+
preSold := sold
8+
sold = held + price
9+
held = max(held, reset-price)
10+
reset = max(reset, preSold)
11+
12+
}
13+
return max(sold, reset)
14+
}
15+
16+
func max(x, y int) int {
17+
if x > y {
18+
return x
19+
}
20+
return y
521
}

src/0309.Best-Time-to-Buy-and-Sell-Stock-with-Cooldown/Solution_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase", []int{1, 2, 3, 0, 2}, 3},
17+
{"TestCase", []int{1}, 0},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := maxProfit(c.inputs)
2524
if !reflect.DeepEqual(got, c.expect) {
2625
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
2726
c.expect, got, c.inputs)

0 commit comments

Comments
 (0)
0