Skip to content

Commit e74483b

Browse files
add golang Solution for problem 213
1 parent 20e100a commit e74483b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func rob(nums []int) int {
2+
l := len(nums)
3+
if l == 0 {
4+
return 0
5+
}
6+
if l == 1 {
7+
return nums[0]
8+
}
9+
res1 := _rob(nums[:l-1])
10+
res2 := _rob(nums[1:])
11+
if res1 < res2 {
12+
return res2
13+
}
14+
return res1
15+
}
16+
17+
func _rob(nums []int) int {
18+
x, y := 0, 0
19+
for _, n := range nums {
20+
x, y = y, x+n
21+
if x > y {
22+
y = x
23+
}
24+
}
25+
return y
26+
}

0 commit comments

Comments
 (0)