Skip to content

Commit b1a7979

Browse files
authored
Merge pull request #988 from 0xff-dev/2343
Add solution and test-cases for problem 2343
2 parents 66cdfeb + 97e9891 commit b1a7979

File tree

3 files changed

+90
-27
lines changed

3 files changed

+90
-27
lines changed

leetcode/2301-2400/2343.Query-Kth-Smallest-Trimmed-Number/README.md

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,44 @@
11
# [2343.Query Kth Smallest Trimmed Number][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 strings `nums`, where each string is of **equal length** and consists of only digits.
5+
6+
You are also given a **0-indexed** 2D integer array `queries` where `queries[i] = [ki, trimi]`. For each `queries[i]`, you need to:
7+
8+
- **Trim** each number in `nums` to its **rightmost** `trimi` digits.
9+
- Determine the **index** of the `kith` smallest trimmed number in `nums`. If two trimmed numbers are equal, the number with the **lower** index is considered to be smaller.
10+
- Reset each number in `nums` to its original length.
11+
12+
Return an array `answer` of the same length as `queries`, where `answer[i]` is the answer to the `ith` query.
13+
14+
**Note**:
15+
16+
- To trim to the rightmost `x` digits means to keep removing the leftmost digit, until only `x` digits remain.
17+
- Strings in `nums` may contain leading zeros.
718

819
**Example 1:**
920

1021
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
22+
Input: nums = ["102","473","251","814"], queries = [[1,1],[2,3],[4,2],[1,2]]
23+
Output: [2,2,1,0]
24+
Explanation:
25+
1. After trimming to the last digit, nums = ["2","3","1","4"]. The smallest number is 1 at index 2.
26+
2. Trimmed to the last 3 digits, nums is unchanged. The 2nd smallest number is 251 at index 2.
27+
3. Trimmed to the last 2 digits, nums = ["02","73","51","14"]. The 4th smallest number is 73.
28+
4. Trimmed to the last 2 digits, the smallest number is 2 at index 0.
29+
Note that the trimmed number "02" is evaluated as 2.
1330
```
1431

15-
## 题意
16-
> ...
17-
18-
## 题解
32+
**Example 2:**
1933

20-
### 思路1
21-
> ...
22-
Query Kth Smallest Trimmed Number
23-
```go
2434
```
25-
35+
Input: nums = ["24","37","96","04"], queries = [[2,1],[2,2]]
36+
Output: [3,0]
37+
Explanation:
38+
1. Trimmed to the last digit, nums = ["4","7","6","4"]. The 2nd smallest number is 4 at index 3.
39+
There are two occurrences of 4, but the one at index 0 is considered smaller than the one at index 3.
40+
2. Trimmed to the last 2 digits, nums is unchanged. The 2nd smallest number is 24.
41+
```
2642

2743
## 结语
2844

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

3-
func Solution(x bool) bool {
4-
return x
3+
func radixSort2343(nums []string, digit int) [][]string {
4+
cache := make([][]string, digit)
5+
cur := len(nums[0]) - 1
6+
for i := 0; i < digit; i++ {
7+
buckets := [10][]string{}
8+
for _, num := range nums {
9+
key := num[cur] - '0'
10+
buckets[key] = append(buckets[key], num)
11+
}
12+
cur--
13+
index := 0
14+
for _, bucket := range buckets {
15+
for _, n := range bucket {
16+
nums[index] = n
17+
index++
18+
}
19+
}
20+
tmp := make([]string, len(nums))
21+
copy(tmp, nums)
22+
cache[i] = tmp
23+
}
24+
return cache
25+
}
26+
27+
func Solution(nums []string, queries [][]int) []int {
28+
maxDigit := 0
29+
indies := make(map[string][]int)
30+
for i, n := range nums {
31+
if _, ok := indies[n]; !ok {
32+
indies[n] = make([]int, 0)
33+
}
34+
indies[n] = append(indies[n], i)
35+
}
36+
for _, q := range queries {
37+
maxDigit = max(maxDigit, q[1])
38+
}
39+
cache := radixSort2343(nums, maxDigit)
40+
ans := make([]int, len(queries))
41+
for i, q := range queries {
42+
sortedArray := cache[q[1]-1]
43+
index := 0
44+
target := sortedArray[q[0]-1]
45+
for pre := q[0] - 2; pre >= 0 && sortedArray[pre] == target; pre-- {
46+
index++
47+
}
48+
ans[i] = indies[target][index]
49+
}
50+
51+
return ans
552
}

leetcode/2301-2400/2343.Query-Kth-Smallest-Trimmed-Number/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
nums []string
14+
queries [][]int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []string{"102", "473", "251", "814"}, [][]int{{1, 1}, {2, 3}, {4, 2}, {1, 2}}, []int{2, 2, 1, 0}},
18+
{"TestCase2", []string{"24", "37", "96", "04"}, [][]int{{2, 1}, {2, 2}}, []int{3, 0}},
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.queries)
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.queries)
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)