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

Commitb858962

Browse files
author
Sathish Babu
committed
✨ Added solution to 198
1 parent61106b8 commitb858962

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

‎src/0198.House-Robber/Solution.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ func rob3(nums []int) int {
3636
returnmax(robEven,robOdd)
3737
}
3838

39+
funcrob4(nums []int)int {
40+
n:=len(nums)
41+
ifn==0 {
42+
return0
43+
}
44+
ifn==1 {
45+
returnnums[0]
46+
}
47+
dp:=make([]int,n)
48+
dp[0],dp[1]=nums[0],max(nums[0],nums[1])
49+
fori:=2;i<n;i++ {
50+
dp[i]=max(nums[i]+dp[i-2],dp[i-1])
51+
}
52+
returndp[n-1]
53+
}
54+
3955
funcmax(x,yint)int {
4056
ifx>y {
4157
returnx

‎src/0198.House-Robber/Solution_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package Solution
22

33
import (
44
"reflect"
5+
"strconv"
56
"testing"
67
)
78

@@ -75,6 +76,32 @@ func TestSolution3(t *testing.T) {
7576
}
7677
}
7778

79+
funcTestSolution4(t*testing.T) {
80+
//测试用例
81+
cases:= []struct {
82+
namestring
83+
inputs []int
84+
expectint
85+
}{
86+
{"TestCacse", []int{},0},
87+
{"TestCacse", []int{3},3},
88+
{"TestCacse", []int{1,2,3,1},4},
89+
{"TestCacse", []int{2,7,9,3,1},12},
90+
{"TestCacse", []int{5,2,6,7,3,1},14},
91+
}
92+
93+
//开始测试
94+
fori,c:=rangecases {
95+
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
96+
got:=rob4(c.inputs)
97+
if!reflect.DeepEqual(got,c.expect) {
98+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
99+
c.expect,got,c.inputs)
100+
}
101+
})
102+
}
103+
}
104+
78105
//压力测试
79106
funcBenchmarkSolution(b*testing.B) {
80107

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp