From c339b5fe8c6fc6d7df672cc9df20c66caace12ae Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 15 Mar 2025 18:36:51 +0800 Subject: [PATCH] Add solution and test-cases for problem 2560 --- .../2501-2600/2560.House-Robber-IV/README.md | 41 +++++++++++++++++++ .../2560.House-Robber-IV/Solution.go | 36 +++++++++++++++- .../2560.House-Robber-IV/Solution_test.go | 20 ++++----- 3 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 leetcode/2501-2600/2560.House-Robber-IV/README.md diff --git a/leetcode/2501-2600/2560.House-Robber-IV/README.md b/leetcode/2501-2600/2560.House-Robber-IV/README.md new file mode 100644 index 000000000..1700846c1 --- /dev/null +++ b/leetcode/2501-2600/2560.House-Robber-IV/README.md @@ -0,0 +1,41 @@ +# [2560.House Robber IV][title] + +## Description + +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**. + +The **capability** of the robber is the maximum amount of money he steals from one house of all the houses he robbed. + +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. + +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. + +Return the **minimum** capability of the robber out of all the possible ways to steal at least k houses. + +**Example 1:** + +``` +Input: nums = [2,3,5,9], k = 2 +Output: 5 +Explanation: +There are three ways to rob at least 2 houses: +- Rob the houses at indices 0 and 2. Capability is max(nums[0], nums[2]) = 5. +- Rob the houses at indices 0 and 3. Capability is max(nums[0], nums[3]) = 9. +- Rob the houses at indices 1 and 3. Capability is max(nums[1], nums[3]) = 9. +Therefore, we return min(5, 9, 9) = 5. +``` + +**Example 2:** + +``` +Input: nums = [2,7,9,3,1], k = 2 +Output: 2 +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. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/house-robber-iv +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2501-2600/2560.House-Robber-IV/Solution.go b/leetcode/2501-2600/2560.House-Robber-IV/Solution.go index d115ccf5e..73677b6b9 100755 --- a/leetcode/2501-2600/2560.House-Robber-IV/Solution.go +++ b/leetcode/2501-2600/2560.House-Robber-IV/Solution.go @@ -1,5 +1,37 @@ package Solution -func Solution(x bool) bool { - return x +import "slices" + +func Solution(nums []int, k int) int { + l := len(nums) + left, right := 0, slices.Max(nums) + + capFn := func(capability int) bool { + count := 0 + i := 0 + + for i < l { + if nums[i] <= capability { + count++ + i++ + if i < l { + i++ + } + } else { + i++ + } + } + return count >= k + } + + for left < right { + mid := (left + right) / 2 + if capFn(mid) { + right = mid + } else { + left = mid + 1 + } + } + + return left } diff --git a/leetcode/2501-2600/2560.House-Robber-IV/Solution_test.go b/leetcode/2501-2600/2560.House-Robber-IV/Solution_test.go index 14ff50eb4..ce1479f84 100755 --- a/leetcode/2501-2600/2560.House-Robber-IV/Solution_test.go +++ b/leetcode/2501-2600/2560.House-Robber-IV/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + nums []int + k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{2, 3, 5, 9}, 2, 5}, + {"TestCase2", []int{2, 7, 9, 3, 1}, 2, 2}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.nums, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }