diff --git a/leetcode/2301-2400/2343.Query-Kth-Smallest-Trimmed-Number/README.md b/leetcode/2301-2400/2343.Query-Kth-Smallest-Trimmed-Number/README.md index e49a1a0c9..626de37bd 100755 --- a/leetcode/2301-2400/2343.Query-Kth-Smallest-Trimmed-Number/README.md +++ b/leetcode/2301-2400/2343.Query-Kth-Smallest-Trimmed-Number/README.md @@ -1,28 +1,44 @@ # [2343.Query Kth Smallest Trimmed Number][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 strings `nums`, where each string is of **equal length** and consists of only digits. + +You are also given a **0-indexed** 2D integer array `queries` where `queries[i] = [ki, trimi]`. For each `queries[i]`, you need to: + +- **Trim** each number in `nums` to its **rightmost** `trimi` digits. +- 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. +- Reset each number in `nums` to its original length. + +Return an array `answer` of the same length as `queries`, where `answer[i]` is the answer to the `ith` query. + +**Note**: + +- To trim to the rightmost `x` digits means to keep removing the leftmost digit, until only `x` digits remain. +- Strings in `nums` may contain leading zeros. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = ["102","473","251","814"], queries = [[1,1],[2,3],[4,2],[1,2]] +Output: [2,2,1,0] +Explanation: +1. After trimming to the last digit, nums = ["2","3","1","4"]. The smallest number is 1 at index 2. +2. Trimmed to the last 3 digits, nums is unchanged. The 2nd smallest number is 251 at index 2. +3. Trimmed to the last 2 digits, nums = ["02","73","51","14"]. The 4th smallest number is 73. +4. Trimmed to the last 2 digits, the smallest number is 2 at index 0. + Note that the trimmed number "02" is evaluated as 2. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Query Kth Smallest Trimmed Number -```go ``` - +Input: nums = ["24","37","96","04"], queries = [[2,1],[2,2]] +Output: [3,0] +Explanation: +1. Trimmed to the last digit, nums = ["4","7","6","4"]. The 2nd smallest number is 4 at index 3. + There are two occurrences of 4, but the one at index 0 is considered smaller than the one at index 3. +2. Trimmed to the last 2 digits, nums is unchanged. The 2nd smallest number is 24. +``` ## 结语 diff --git a/leetcode/2301-2400/2343.Query-Kth-Smallest-Trimmed-Number/Solution.go b/leetcode/2301-2400/2343.Query-Kth-Smallest-Trimmed-Number/Solution.go index d115ccf5e..5a8faa9f4 100644 --- a/leetcode/2301-2400/2343.Query-Kth-Smallest-Trimmed-Number/Solution.go +++ b/leetcode/2301-2400/2343.Query-Kth-Smallest-Trimmed-Number/Solution.go @@ -1,5 +1,52 @@ package Solution -func Solution(x bool) bool { - return x +func radixSort2343(nums []string, digit int) [][]string { + cache := make([][]string, digit) + cur := len(nums[0]) - 1 + for i := 0; i < digit; i++ { + buckets := [10][]string{} + for _, num := range nums { + key := num[cur] - '0' + buckets[key] = append(buckets[key], num) + } + cur-- + index := 0 + for _, bucket := range buckets { + for _, n := range bucket { + nums[index] = n + index++ + } + } + tmp := make([]string, len(nums)) + copy(tmp, nums) + cache[i] = tmp + } + return cache +} + +func Solution(nums []string, queries [][]int) []int { + maxDigit := 0 + indies := make(map[string][]int) + for i, n := range nums { + if _, ok := indies[n]; !ok { + indies[n] = make([]int, 0) + } + indies[n] = append(indies[n], i) + } + for _, q := range queries { + maxDigit = max(maxDigit, q[1]) + } + cache := radixSort2343(nums, maxDigit) + ans := make([]int, len(queries)) + for i, q := range queries { + sortedArray := cache[q[1]-1] + index := 0 + target := sortedArray[q[0]-1] + for pre := q[0] - 2; pre >= 0 && sortedArray[pre] == target; pre-- { + index++ + } + ans[i] = indies[target][index] + } + + return ans } diff --git a/leetcode/2301-2400/2343.Query-Kth-Smallest-Trimmed-Number/Solution_test.go b/leetcode/2301-2400/2343.Query-Kth-Smallest-Trimmed-Number/Solution_test.go index 14ff50eb4..b877bb7c3 100644 --- a/leetcode/2301-2400/2343.Query-Kth-Smallest-Trimmed-Number/Solution_test.go +++ b/leetcode/2301-2400/2343.Query-Kth-Smallest-Trimmed-Number/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + nums []string + queries [][]int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []string{"102", "473", "251", "814"}, [][]int{{1, 1}, {2, 3}, {4, 2}, {1, 2}}, []int{2, 2, 1, 0}}, + {"TestCase2", []string{"24", "37", "96", "04"}, [][]int{{2, 1}, {2, 2}}, []int{3, 0}}, } // 开始测试 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.queries) 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.queries) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }