Movatterモバイル変換


[0]ホーム

URL:


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

Commit888f681

Browse files
committed
add 309 solution
1 parent27df565 commit888f681

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-
funcSolution(xbool)bool {
4-
returnx
3+
funcmaxProfit(prices []int)int {
4+
sold,held,reset:=-1<<31,-1<<31,0
5+
6+
for_,price:=rangeprices {
7+
preSold:=sold
8+
sold=held+price
9+
held=max(held,reset-price)
10+
reset=max(reset,preSold)
11+
12+
}
13+
returnmax(sold,reset)
14+
}
15+
16+
funcmax(x,yint)int {
17+
ifx>y {
18+
returnx
19+
}
20+
returny
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
namestring
13-
inputsbool
14-
expectbool
13+
inputs[]int
14+
expectint
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
fori,c:=rangecases {
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)

[8]ページ先頭

©2009-2025 Movatter.jp