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

Commit5901952

Browse files
committed
add 198 problem solution
1 parentff0c001 commit5901952

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

‎src/0198.House-Robber/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#[198.House Robber][title]
2+
3+
##Description
4+
5+
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
6+
7+
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
8+
**Example 1:**
9+
10+
```
11+
Input: [1,2,3,1]
12+
Output: 4
13+
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
14+
Total amount you can rob = 1 + 3 = 4.
15+
```
16+
17+
**Example 2:**
18+
19+
```
20+
Input: [2,7,9,3,1]
21+
Output: 12
22+
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
23+
Total amount you can rob = 2 + 9 + 1 = 12.
24+
```
25+
26+
**Tags:** Math, String
27+
28+
##题意
29+
>输入一个整数序列,然后求出任意不相邻的数加起来的最大的和
30+
31+
##题解
32+
33+
###思路1
34+
>找到公式用动态规划
35+
```sh
36+
f(0) = nums[0]
37+
f(1) = max(f(0), f(1))
38+
f(2) = max(nums[2] + f(0), f(1))
39+
f(3) = max(nums[3] + f(1), f(2))
40+
.
41+
.
42+
.
43+
f(n) = max(nums[n] + f(n-2), f(n-1))
44+
```
45+
46+
47+
```go
48+
funcrob(nums []int)int {
49+
nLenghth:=len(nums)
50+
51+
dp:=make([][2]int, nLenghth+1)
52+
fori:=1; i <= nLenghth; i++ {
53+
dp[i][0] =Max(dp[i-1][0], dp[i-1][1])
54+
dp[i][1] = nums[i-1] + dp[i-1][0]
55+
}
56+
returnMax(dp[nLenghth][0], dp[nLenghth][1])
57+
}
58+
```
59+
60+
###思路2
61+
>任意不相邻,其实这个数是可以传递的
62+
```go
63+
funcrob(nums []int)int {
64+
prevNo,prevYes:=0,0
65+
66+
for_,v:=range nums {
67+
tmp:= prevNo
68+
prevNo =Max(prevNo, prevYes)
69+
prevYes = v + tmp
70+
}
71+
returnMax(prevNo, prevYes)
72+
}
73+
```
74+
75+
##结语
76+
77+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
78+
79+
[title]:https://leetcode.com/problems/house-robber/
80+
[me]:https://github.com/kylesliu/awesome-golang-leetcode

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Solution
2+
3+
//普通的动态规划
4+
funcrob(nums []int)int {
5+
nLenghth:=len(nums)
6+
7+
dp:=make([][2]int,nLenghth+1)
8+
fori:=1;i<=nLenghth;i++ {
9+
dp[i][0]=Max(dp[i-1][0],dp[i-1][1])
10+
dp[i][1]=nums[i-1]+dp[i-1][0]
11+
}
12+
//fmt.Println(dp)
13+
returnMax(dp[nLenghth][0],dp[nLenghth][1])
14+
}
15+
16+
//优化空间
17+
funcrob2(nums []int)int {
18+
prevNo,prevYes:=0,0
19+
20+
for_,v:=rangenums {
21+
tmp:=prevNo
22+
prevNo=Max(prevNo,prevYes)
23+
prevYes=v+tmp
24+
}
25+
returnMax(prevNo,prevYes)
26+
}
27+
28+
funcMax(x,yint)int {
29+
ifx>y {
30+
returnx
31+
}
32+
returny
33+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
funcTestSolution(t*testing.T) {
9+
//测试用例
10+
cases:= []struct {
11+
namestring
12+
inputs []int
13+
expectint
14+
}{
15+
{"TestCacse 1", []int{1,2,3,1},4},
16+
{"TestCacse 1", []int{2,7,9,3,1},12},
17+
}
18+
19+
//开始测试
20+
for_,c:=rangecases {
21+
t.Run(c.name,func(t*testing.T) {
22+
got:=rob(c.inputs)
23+
if!reflect.DeepEqual(got,c.expect) {
24+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
25+
c.expect,got,c.inputs)
26+
}
27+
})
28+
}
29+
}
30+
31+
funcTestSolution2(t*testing.T) {
32+
//测试用例
33+
cases:= []struct {
34+
namestring
35+
inputs []int
36+
expectint
37+
}{
38+
{"TestCacse 1", []int{1,2,3,1},4},
39+
{"TestCacse 1", []int{2,7,9,3,1},12},
40+
}
41+
42+
//开始测试
43+
for_,c:=rangecases {
44+
t.Run(c.name,func(t*testing.T) {
45+
got:=rob2(c.inputs)
46+
if!reflect.DeepEqual(got,c.expect) {
47+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
48+
c.expect,got,c.inputs)
49+
}
50+
})
51+
}
52+
}
53+
54+
//压力测试
55+
funcBenchmarkSolution(b*testing.B) {
56+
57+
}
58+
59+
//使用案列
60+
funcExampleSolution() {
61+
62+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp