Skip to content

Commit 2dbe1db

Browse files
committed
Add solution and test-cases for problem 2948
1 parent d569e0c commit 2dbe1db

File tree

3 files changed

+85
-25
lines changed

3 files changed

+85
-25
lines changed

leetcode/2901-3000/2948.Make-Lexicographically-Smallest-Array-by-Swapping-Elements/README.md

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
# [2948.Make Lexicographically Smallest Array by Swapping Elements][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a **0-indexed** array of **positive** integers `nums` and a **positive** integer `limit`.
5+
6+
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`.
7+
8+
Return the **lexicographically smallest array** that can be obtained by performing the operation any number of times.
9+
10+
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`.
11+
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: nums = [1,5,3,9,8], limit = 2
17+
Output: [1,3,5,8,9]
18+
Explanation: Apply the operation 2 times:
19+
- Swap nums[1] with nums[2]. The array becomes [1,3,5,9,8]
20+
- Swap nums[3] with nums[4]. The array becomes [1,3,5,8,9]
21+
We cannot obtain a lexicographically smaller array by applying any more operations.
22+
Note that it may be possible to get the same result by doing different operations.
1323
```
1424

15-
## 题意
16-
> ...
25+
**Example 2:**
1726

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Make Lexicographically Smallest Array by Swapping Elements
23-
```go
2427
```
28+
Input: nums = [1,7,6,18,2,1], limit = 3
29+
Output: [1,6,7,18,1,2]
30+
Explanation: Apply the operation 3 times:
31+
- Swap nums[1] with nums[2]. The array becomes [1,6,7,18,2,1]
32+
- Swap nums[0] with nums[4]. The array becomes [2,6,7,18,1,1]
33+
- Swap nums[0] with nums[5]. The array becomes [1,6,7,18,1,2]
34+
We cannot obtain a lexicographically smaller array by applying any more operations.
35+
```
36+
37+
**Example 3:**
2538

39+
```
40+
Input: nums = [1,7,28,19,10], limit = 3
41+
Output: [1,7,28,19,10]
42+
Explanation: [1,7,28,19,10] is the lexicographically smallest array we can obtain because we cannot apply the operation on any two indices.
43+
```
2644

2745
## 结语
2846

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums []int, limit int) []int {
6+
l := len(nums)
7+
dst := make([]int, l)
8+
for i := range l {
9+
dst[i] = i
10+
}
11+
sort.Slice(dst, func(i, j int) bool {
12+
a, b := nums[dst[i]], nums[dst[j]]
13+
if a == b {
14+
return i < j
15+
}
16+
return a < b
17+
})
18+
groupIndies := map[int][]int{
19+
0: []int{1, dst[0]},
20+
}
21+
numGroup := make([]int, len(nums))
22+
23+
g := 0
24+
25+
for i := 1; i < l; i++ {
26+
if diff := nums[dst[i]] - nums[dst[i-1]]; diff > limit {
27+
g++
28+
}
29+
30+
numGroup[dst[i]] = g
31+
if _, ok := groupIndies[g]; !ok {
32+
groupIndies[g] = []int{1}
33+
}
34+
groupIndies[g] = append(groupIndies[g], dst[i])
35+
}
36+
37+
r := make([]int, l)
38+
for i := range nums {
39+
group := numGroup[i]
40+
indies := groupIndies[group]
41+
idx := indies[indies[0]]
42+
indies[0]++
43+
r[i] = nums[idx]
44+
}
45+
return r
546
}

leetcode/2901-3000/2948.Make-Lexicographically-Smallest-Array-by-Swapping-Elements/Solution_test.go

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

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.nums, c.limit)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.nums, c.limit)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)