Skip to content

Commit c339b5f

Browse files
committed
Add solution and test-cases for problem 2560
1 parent f0a9eb1 commit c339b5f

File tree

3 files changed

+85
-12
lines changed

3 files changed

+85
-12
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# [2560.House Robber IV][title]
2+
3+
## Description
4+
5+
There are several consecutive houses along a street, each of which has some money inside. There is also a robber, who wants to steal money from the homes, but he **refuses to steal from adjacent homes**.
6+
7+
The **capability** of the robber is the maximum amount of money he steals from one house of all the houses he robbed.
8+
9+
You are given an integer array `nums` representing how much money is stashed in each house. More formally, the `ith` house from the left has `nums[i]` dollars.
10+
11+
You are also given an integer `k`, representing the **minimum** number of houses the robber will steal from. It is always possible to steal at least k houses.
12+
13+
Return the **minimum** capability of the robber out of all the possible ways to steal at least k houses.
14+
15+
**Example 1:**
16+
17+
```
18+
Input: nums = [2,3,5,9], k = 2
19+
Output: 5
20+
Explanation:
21+
There are three ways to rob at least 2 houses:
22+
- Rob the houses at indices 0 and 2. Capability is max(nums[0], nums[2]) = 5.
23+
- Rob the houses at indices 0 and 3. Capability is max(nums[0], nums[3]) = 9.
24+
- Rob the houses at indices 1 and 3. Capability is max(nums[1], nums[3]) = 9.
25+
Therefore, we return min(5, 9, 9) = 5.
26+
```
27+
28+
**Example 2:**
29+
30+
```
31+
Input: nums = [2,7,9,3,1], k = 2
32+
Output: 2
33+
Explanation: There are 7 ways to rob the houses. The way which leads to minimum capability is to rob the house at index 0 and 4. Return max(nums[0], nums[4]) = 2.
34+
```
35+
36+
## 结语
37+
38+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
39+
40+
[title]: https://leetcode.com/problems/house-robber-iv
41+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "slices"
4+
5+
func Solution(nums []int, k int) int {
6+
l := len(nums)
7+
left, right := 0, slices.Max(nums)
8+
9+
capFn := func(capability int) bool {
10+
count := 0
11+
i := 0
12+
13+
for i < l {
14+
if nums[i] <= capability {
15+
count++
16+
i++
17+
if i < l {
18+
i++
19+
}
20+
} else {
21+
i++
22+
}
23+
}
24+
return count >= k
25+
}
26+
27+
for left < right {
28+
mid := (left + right) / 2
29+
if capFn(mid) {
30+
right = mid
31+
} else {
32+
left = mid + 1
33+
}
34+
}
35+
36+
return left
537
}

leetcode/2501-2600/2560.House-Robber-IV/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
nums []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{2, 3, 5, 9}, 2, 5},
18+
{"TestCase2", []int{2, 7, 9, 3, 1}, 2, 2},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.nums, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.nums, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)