diff --git a/leetcode/2901-3000/2948.Make-Lexicographically-Smallest-Array-by-Swapping-Elements/README.md b/leetcode/2901-3000/2948.Make-Lexicographically-Smallest-Array-by-Swapping-Elements/README.md index efc2d439c..fbc5d2d7f 100755 --- a/leetcode/2901-3000/2948.Make-Lexicographically-Smallest-Array-by-Swapping-Elements/README.md +++ b/leetcode/2901-3000/2948.Make-Lexicographically-Smallest-Array-by-Swapping-Elements/README.md @@ -1,28 +1,46 @@ # [2948.Make Lexicographically Smallest Array by Swapping Elements][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 a **0-indexed** array of **positive** integers `nums` and a **positive** integer `limit`. + +In one operation, you can choose any two indices `i` and `j` and swap `nums[i]` and `nums[j]` if `|nums[i] - nums[j]| <= limit`. + +Return the **lexicographically smallest array** that can be obtained by performing the operation any number of times. + +An array `a` is lexicographically smaller than an array `b` if in the first position where `a` and `b` differ, array a has an element that is less than the corresponding element in `b`. For example, the array `[2,10,3]` is lexicographically smaller than the array `[10,2,3]` because they differ at index `0` and `2 < 10`. + **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [1,5,3,9,8], limit = 2 +Output: [1,3,5,8,9] +Explanation: Apply the operation 2 times: +- Swap nums[1] with nums[2]. The array becomes [1,3,5,9,8] +- Swap nums[3] with nums[4]. The array becomes [1,3,5,8,9] +We cannot obtain a lexicographically smaller array by applying any more operations. +Note that it may be possible to get the same result by doing different operations. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Make Lexicographically Smallest Array by Swapping Elements -```go ``` +Input: nums = [1,7,6,18,2,1], limit = 3 +Output: [1,6,7,18,1,2] +Explanation: Apply the operation 3 times: +- Swap nums[1] with nums[2]. The array becomes [1,6,7,18,2,1] +- Swap nums[0] with nums[4]. The array becomes [2,6,7,18,1,1] +- Swap nums[0] with nums[5]. The array becomes [1,6,7,18,1,2] +We cannot obtain a lexicographically smaller array by applying any more operations. +``` + +**Example 3:** +``` +Input: nums = [1,7,28,19,10], limit = 3 +Output: [1,7,28,19,10] +Explanation: [1,7,28,19,10] is the lexicographically smallest array we can obtain because we cannot apply the operation on any two indices. +``` ## 结语 diff --git a/leetcode/2901-3000/2948.Make-Lexicographically-Smallest-Array-by-Swapping-Elements/Solution.go b/leetcode/2901-3000/2948.Make-Lexicographically-Smallest-Array-by-Swapping-Elements/Solution.go index d115ccf5e..63cb46dd2 100644 --- a/leetcode/2901-3000/2948.Make-Lexicographically-Smallest-Array-by-Swapping-Elements/Solution.go +++ b/leetcode/2901-3000/2948.Make-Lexicographically-Smallest-Array-by-Swapping-Elements/Solution.go @@ -1,5 +1,46 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(nums []int, limit int) []int { + l := len(nums) + dst := make([]int, l) + for i := range l { + dst[i] = i + } + sort.Slice(dst, func(i, j int) bool { + a, b := nums[dst[i]], nums[dst[j]] + if a == b { + return i < j + } + return a < b + }) + groupIndies := map[int][]int{ + 0: []int{1, dst[0]}, + } + numGroup := make([]int, len(nums)) + + g := 0 + + for i := 1; i < l; i++ { + if diff := nums[dst[i]] - nums[dst[i-1]]; diff > limit { + g++ + } + + numGroup[dst[i]] = g + if _, ok := groupIndies[g]; !ok { + groupIndies[g] = []int{1} + } + groupIndies[g] = append(groupIndies[g], dst[i]) + } + + r := make([]int, l) + for i := range nums { + group := numGroup[i] + indies := groupIndies[group] + idx := indies[indies[0]] + indies[0]++ + r[i] = nums[idx] + } + return r } diff --git a/leetcode/2901-3000/2948.Make-Lexicographically-Smallest-Array-by-Swapping-Elements/Solution_test.go b/leetcode/2901-3000/2948.Make-Lexicographically-Smallest-Array-by-Swapping-Elements/Solution_test.go index 14ff50eb4..15be10753 100644 --- a/leetcode/2901-3000/2948.Make-Lexicographically-Smallest-Array-by-Swapping-Elements/Solution_test.go +++ b/leetcode/2901-3000/2948.Make-Lexicographically-Smallest-Array-by-Swapping-Elements/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + nums []int + limit int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 5, 3, 9, 8}, 2, []int{1, 3, 5, 8, 9}}, + {"TestCase2", []int{1, 7, 6, 18, 2, 1}, 3, []int{1, 6, 7, 18, 1, 2}}, + {"TestCase3", []int{1, 7, 28, 19, 10}, 3, []int{1, 7, 28, 19, 10}}, } // 开始测试 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.limit) 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.limit) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }