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

Commit 2e14d5d

Browse files
committed
update
1 parent 4e48bac commit 2e14d5d

File tree

1 file changed

+28
-2
lines changed
  • problems/0213.house-robber-ii

1 file changed

+28
-2
lines changed

‎problems/0213.house-robber-ii/run.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
package ii
22

3-
funcRun() {
3+
import"fmt"
44

5+
func Run() {
6+
nums := []int{1, 2, 3, 1}
7+
sum := rob(nums)
8+
fmt.Println(sum)
59
}
610

711
func rob(nums []int) int {
12+
if len(nums) == 0 {
13+
return 0
14+
}
15+
if len(nums) == 1 {
16+
return nums[0]
17+
}
18+
return max(myRob(nums[0:len(nums)-1]), myRob(nums[1:]))
19+
}
20+
21+
func myRob(nums []int) int {
22+
pre, cur, tmp := 0, 0, 0
23+
for _, num := range nums {
24+
tmp = cur
25+
cur = max(pre+num, cur)
26+
pre = tmp
27+
}
28+
return cur
29+
}
830

9-
return 0
31+
func max(a, b int) int {
32+
if a > b {
33+
return a
34+
}
35+
return b
1036
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /