From 521bbeaeb986ace28dbfa4e24b4f2a92783dfd2b Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 29 Apr 2025 09:12:33 +0800 Subject: [PATCH] Add solution and test-cases for problem 2294 --- .../README.md | 46 +++++++++++++------ .../Solution.go | 17 ++++++- .../Solution_test.go | 21 +++++---- 3 files changed, 59 insertions(+), 25 deletions(-) diff --git a/leetcode/2201-2300/2294.Partition-Array-Such-That-Maximum-Difference-Is-K/README.md b/leetcode/2201-2300/2294.Partition-Array-Such-That-Maximum-Difference-Is-K/README.md index 0cc0092be..5ed9dd9f5 100755 --- a/leetcode/2201-2300/2294.Partition-Array-Such-That-Maximum-Difference-Is-K/README.md +++ b/leetcode/2201-2300/2294.Partition-Array-Such-That-Maximum-Difference-Is-K/README.md @@ -1,28 +1,48 @@ # [2294.Partition Array Such That Maximum Difference Is K][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given an integer array `nums` and an integer `k`. You may partition `nums` into one or more **subsequences** such that each element in `nums` appears in **exactly** one of the subsequences. + +Return the **minimum** number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is **at most** `k`. + +A **subsequence** is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [3,6,1,2,5], k = 2 +Output: 2 +Explanation: +We can partition nums into the two subsequences [3,1,2] and [6,5]. +The difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2. +The difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1. +Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Partition Array Such That Maximum Difference Is K -```go ``` +Input: nums = [1,2,3], k = 1 +Output: 2 +Explanation: +We can partition nums into the two subsequences [1,2] and [3]. +The difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1. +The difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0. +Since two subsequences were created, we return 2. Note that another optimal solution is to partition nums into the two subsequences [1] and [2,3]. +``` + +**Example 3:** +``` +Input: nums = [2,2,4,5], k = 0 +Output: 3 +Explanation: +We can partition nums into the three subsequences [2,2], [4], and [5]. +The difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0. +The difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0. +The difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0. +Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed. +``` ## 结语 diff --git a/leetcode/2201-2300/2294.Partition-Array-Such-That-Maximum-Difference-Is-K/Solution.go b/leetcode/2201-2300/2294.Partition-Array-Such-That-Maximum-Difference-Is-K/Solution.go index d115ccf5e..2a1979f76 100644 --- a/leetcode/2201-2300/2294.Partition-Array-Such-That-Maximum-Difference-Is-K/Solution.go +++ b/leetcode/2201-2300/2294.Partition-Array-Such-That-Maximum-Difference-Is-K/Solution.go @@ -1,5 +1,18 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(nums []int, k int) int { + sort.Ints(nums) + ans := 0 + start := 0 + l := len(nums) + for start < l { + i := sort.Search(l, func(i int) bool { + return nums[i] > nums[start]+k + }) + ans++ + start = i + } + return ans } diff --git a/leetcode/2201-2300/2294.Partition-Array-Such-That-Maximum-Difference-Is-K/Solution_test.go b/leetcode/2201-2300/2294.Partition-Array-Such-That-Maximum-Difference-Is-K/Solution_test.go index 14ff50eb4..f54c0e956 100644 --- a/leetcode/2201-2300/2294.Partition-Array-Such-That-Maximum-Difference-Is-K/Solution_test.go +++ b/leetcode/2201-2300/2294.Partition-Array-Such-That-Maximum-Difference-Is-K/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{3, 6, 1, 2, 5}, 2, 2}, + {"TestCase2", []int{1, 2, 3}, 1, 2}, + {"TestCase3", []int{2, 2, 4, 5}, 0, 3}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, 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.inputs, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }