Skip to content

Commit 8271742

Browse files
committed
+ problem 2343
1 parent 17a9d2c commit 8271742

File tree

5 files changed

+203
-0
lines changed

5 files changed

+203
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# 2343. Query Kth Smallest Trimmed Number
2+
You are given a **0-indexed** array of strings `nums`, where each string is of **equal length** and consists of only digits.
3+
4+
You are also given a **0-indexed** 2D integer array `queries` where <code>queries[i] = [k<sub>i</sub>, trim<sub>i</sub>]</code>. For each `queries[i]`, you need to:
5+
6+
* **Trim** each number in `nums` to its **rightmost** <code>trim<sub>i</sub></code> digits.
7+
* Determine the **index** of the <code>k<sub>i</sub><sup>th</sup></code> smallest trimmed number in `nums`. If two trimmed numbers are equal, the number with the **lower** index is considered to be smaller.
8+
* Reset each number in `nums` to its original length.
9+
10+
Return *an array* `answer` *of the same length as* `queries`, *where* `answer[i]` *is the answer to the* <code>i<sup>th</sup></code> *query*.
11+
12+
**Note**:
13+
14+
* To trim to the rightmost `x` digits means to keep removing the leftmost digit, until only `x` digits remain.
15+
* Strings in `nums` may contain leading zeros.
16+
17+
#### Example 1:
18+
<pre>
19+
<strong>Input:</strong> nums = ["102","473","251","814"], queries = [[1,1],[2,3],[4,2],[1,2]]
20+
<strong>Output:</strong> [2,2,1,0]
21+
<strong>Explanation:</strong>
22+
1. After trimming to the last digit, nums = ["2","3","1","4"]. The smallest number is 1 at index 2.
23+
2. Trimmed to the last 3 digits, nums is unchanged. The 2nd smallest number is 251 at index 2.
24+
3. Trimmed to the last 2 digits, nums = ["02","73","51","14"]. The 4th smallest number is 73.
25+
4. Trimmed to the last 2 digits, the smallest number is 2 at index 0.
26+
Note that the trimmed number "02" is evaluated as 2.
27+
</pre>
28+
29+
#### Example 2:
30+
<pre>
31+
<strong>Input:</strong> nums = ["24","37","96","04"], queries = [[2,1],[2,2]]
32+
<strong>Output:</strong> [3,0]
33+
<strong>Explanation:</strong>
34+
1. Trimmed to the last digit, nums = ["4","7","6","4"]. The 2nd smallest number is 4 at index 3.
35+
There are two occurrences of 4, but the one at index 0 is considered smaller than the one at index 3.
36+
2. Trimmed to the last 2 digits, nums is unchanged. The 2nd smallest number is 24.
37+
</pre>
38+
39+
#### Constraints:
40+
* `1 <= nums.length <= 100`
41+
* `1 <= nums[i].length <= 100`
42+
* `nums[i]` consists of only digits.
43+
* All `nums[i].length` are **equal**.
44+
* `1 <= queries.length <= 100`
45+
* `queries[i].length == 2`
46+
* <code>1 <= k<sub>i</sub> <= nums.length</code>
47+
* <code>1 <= trim<sub>i</sub> <= nums[i].length</code>
48+
49+
**Follow up:** Could you use the **Radix Sort Algorithm** to solve this problem? What will be the complexity of that solution?
50+
51+
## Solutions (Python)
52+
53+
### 1. Solution
54+
```Python
55+
class Solution:
56+
def smallestTrimmedNumbers(self, nums: List[str], queries: List[List[int]]) -> List[int]:
57+
nums = [(nums[i], i) for i in range(len(nums))]
58+
trims = {}
59+
answer = [0] * len(queries)
60+
61+
for i in range(len(queries)):
62+
if queries[i][1] not in trims:
63+
trims[queries[i][1]] = []
64+
trims[queries[i][1]].append((i, queries[i][0] - 1))
65+
66+
for trim in range(1, len(nums[0][0]) + 1):
67+
count = [0] * 10
68+
tmp = [("", 0)] * len(nums)
69+
70+
for num, _ in nums:
71+
count[ord(num[-trim]) - 48] += 1
72+
for i in range(1, 10):
73+
count[i] += count[i - 1]
74+
for num, i in nums[::-1]:
75+
count[ord(num[-trim]) - 48] -= 1
76+
tmp[count[ord(num[-trim]) - 48]] = (num, i)
77+
78+
for i, k in trims.get(trim, []):
79+
answer[i] = tmp[k][1]
80+
81+
nums = tmp
82+
83+
return answer
84+
```
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# 2343. 裁剪数字后查询第 K 小的数字
2+
给你一个下标从 **0** 开始的字符串数组 `nums` ,其中每个字符串 **长度相等** 且只包含数字。
3+
4+
再给你一个下标从 **0** 开始的二维整数数组 `queries` ,其中 <code>queries[i] = [k<sub>i</sub>, trim<sub>i</sub>]</code> 。对于每个 `queries[i]` ,你需要:
5+
6+
*`nums` 中每个数字 **裁剪** 到剩下 **最右边** <code>trim<sub>i</sub></code> 个数位。
7+
* 在裁剪过后的数字中,找到 `nums` 中第 <code>k<sub>i</sub></code> 小数字对应的 **下标** 。如果两个裁剪后数字一样大,那么下标 **更小** 的数字视为更小的数字。
8+
*`nums` 中每个数字恢复到原本字符串。
9+
10+
请你返回一个长度与 `queries` 相等的数组 `answer`,其中 `answer[i]`是第 `i` 次查询的结果。
11+
12+
**提示:**
13+
14+
* 裁剪到剩下最右边 `x` 个数位的意思是不断删除最左边的数位,直到剩下 `x` 个数位。
15+
* `nums` 中的字符串可能会有前导 0 。
16+
17+
#### 示例 1:
18+
<pre>
19+
<strong>输入:</strong> nums = ["102","473","251","814"], queries = [[1,1],[2,3],[4,2],[1,2]]
20+
<strong>输出:</strong> [2,2,1,0]
21+
<strong>解释:</strong>
22+
1. 裁剪到只剩 1 个数位后,nums = ["2","3","1","4"] 。最小的数字是 1 ,下标为 2 。
23+
2. 裁剪到剩 3 个数位后,nums 没有变化。第 2 小的数字是 251 ,下标为 2 。
24+
3. 裁剪到剩 2 个数位后,nums = ["02","73","51","14"] 。第 4 小的数字是 73 ,下标为 1 。
25+
4. 裁剪到剩 2 个数位后,最小数字是 2 ,下标为 0 。
26+
注意,裁剪后数字 "02" 值为 2 。
27+
</pre>
28+
29+
#### 示例 2:
30+
<pre>
31+
<strong>输入:</strong> nums = ["24","37","96","04"], queries = [[2,1],[2,2]]
32+
<strong>输出:</strong> [3,0]
33+
<strong>解释:</strong>
34+
1. 裁剪到剩 1 个数位,nums = ["4","7","6","4"] 。第 2 小的数字是 4 ,下标为 3 。
35+
有两个 4 ,下标为 0 的 4 视为小于下标为 3 的 4 。
36+
2. 裁剪到剩 2 个数位,nums 不变。第二小的数字是 24 ,下标为 0 。
37+
</pre>
38+
39+
#### 提示:
40+
* `1 <= nums.length <= 100`
41+
* `1 <= nums[i].length <= 100`
42+
* `nums[i]` 只包含数字。
43+
* 所有 `nums[i].length` 的长度 **相同**
44+
* `1 <= queries.length <= 100`
45+
* `queries[i].length == 2`
46+
* <code>1 <= k<sub>i</sub> <= nums.length</code>
47+
* <code>1 <= trim<sub>i</sub> <= nums[i].length</code>
48+
49+
**进阶:**你能使用 **基数排序算法** 解决此问题吗?这种解法的复杂度又是多少?
50+
51+
## 题解 (Python)
52+
53+
### 1. 题解
54+
```Python
55+
class Solution:
56+
def smallestTrimmedNumbers(self, nums: List[str], queries: List[List[int]]) -> List[int]:
57+
nums = [(nums[i], i) for i in range(len(nums))]
58+
trims = {}
59+
answer = [0] * len(queries)
60+
61+
for i in range(len(queries)):
62+
if queries[i][1] not in trims:
63+
trims[queries[i][1]] = []
64+
trims[queries[i][1]].append((i, queries[i][0] - 1))
65+
66+
for trim in range(1, len(nums[0][0]) + 1):
67+
count = [0] * 10
68+
tmp = [("", 0)] * len(nums)
69+
70+
for num, _ in nums:
71+
count[ord(num[-trim]) - 48] += 1
72+
for i in range(1, 10):
73+
count[i] += count[i - 1]
74+
for num, i in nums[::-1]:
75+
count[ord(num[-trim]) - 48] -= 1
76+
tmp[count[ord(num[-trim]) - 48]] = (num, i)
77+
78+
for i, k in trims.get(trim, []):
79+
answer[i] = tmp[k][1]
80+
81+
nums = tmp
82+
83+
return answer
84+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def smallestTrimmedNumbers(self, nums: List[str], queries: List[List[int]]) -> List[int]:
3+
nums = [(nums[i], i) for i in range(len(nums))]
4+
trims = {}
5+
answer = [0] * len(queries)
6+
7+
for i in range(len(queries)):
8+
if queries[i][1] not in trims:
9+
trims[queries[i][1]] = []
10+
trims[queries[i][1]].append((i, queries[i][0] - 1))
11+
12+
for trim in range(1, len(nums[0][0]) + 1):
13+
count = [0] * 10
14+
tmp = [("", 0)] * len(nums)
15+
16+
for num, _ in nums:
17+
count[ord(num[-trim]) - 48] += 1
18+
for i in range(1, 10):
19+
count[i] += count[i - 1]
20+
for num, i in nums[::-1]:
21+
count[ord(num[-trim]) - 48] -= 1
22+
tmp[count[ord(num[-trim]) - 48]] = (num, i)
23+
24+
for i, k in trims.get(trim, []):
25+
answer[i] = tmp[k][1]
26+
27+
nums = tmp
28+
29+
return answer

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,7 @@
11361136
[2337][2337l]|[Move Pieces to Obtain a String][2337] |![rs]
11371137
[2341][2341l]|[Maximum Number of Pairs in Array][2341] |![rs]
11381138
[2342][2342l]|[Max Sum of a Pair With Equal Sum of Digits][2342] |![rs]
1139+
[2343][2343l]|[Query Kth Smallest Trimmed Number][2343] |![py]
11391140
[2347][2347l]|[Best Poker Hand][2347] |![py]
11401141
[2348][2348l]|[Number of Zero-Filled Subarrays][2348] |![rs]
11411142
[2349][2349l]|[Design a Number Container System][2349] |![rs]
@@ -2383,6 +2384,7 @@
23832384
[2337]:Problemset/2337-Move%20Pieces%20to%20Obtain%20a%20String/README.md#2337-move-pieces-to-obtain-a-string
23842385
[2341]:Problemset/2341-Maximum%20Number%20of%20Pairs%20in%20Array/README.md#2341-maximum-number-of-pairs-in-array
23852386
[2342]:Problemset/2342-Max%20Sum%20of%20a%20Pair%20With%20Equal%20Sum%20of%20Digits/README.md#2342-max-sum-of-a-pair-with-equal-sum-of-digits
2387+
[2343]:Problemset/2343-Query%20Kth%20Smallest%20Trimmed%20Number/README.md#2343-query-kth-smallest-trimmed-number
23862388
[2347]:Problemset/2347-Best%20Poker%20Hand/README.md#2347-best-poker-hand
23872389
[2348]:Problemset/2348-Number%20of%20Zero-Filled%20Subarrays/README.md#2348-number-of-zero-filled-subarrays
23882390
[2349]:Problemset/2349-Design%20a%20Number%20Container%20System/README.md#2349-design-a-number-container-system
@@ -3633,6 +3635,7 @@
36333635
[2337l]:https://leetcode.com/problems/move-pieces-to-obtain-a-string/
36343636
[2341l]:https://leetcode.com/problems/maximum-number-of-pairs-in-array/
36353637
[2342l]:https://leetcode.com/problems/max-sum-of-a-pair-with-equal-sum-of-digits/
3638+
[2343l]:https://leetcode.com/problems/query-kth-smallest-trimmed-number/
36363639
[2347l]:https://leetcode.com/problems/best-poker-hand/
36373640
[2348l]:https://leetcode.com/problems/number-of-zero-filled-subarrays/
36383641
[2349l]:https://leetcode.com/problems/design-a-number-container-system/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,7 @@
11361136
[2337][2337l]|[移动片段得到字符串][2337] |![rs]
11371137
[2341][2341l]|[数组能形成多少数对][2341] |![rs]
11381138
[2342][2342l]|[数位和相等数对的最大和][2342] |![rs]
1139+
[2343][2343l]|[裁剪数字后查询第 K 小的数字][2343] |![py]
11391140
[2347][2347l]|[最好的扑克手牌][2347] |![py]
11401141
[2348][2348l]|[全 0 子数组的数目][2348] |![rs]
11411142
[2349][2349l]|[设计数字容器系统][2349] |![rs]
@@ -2383,6 +2384,7 @@
23832384
[2337]:Problemset/2337-Move%20Pieces%20to%20Obtain%20a%20String/README_CN.md#2337-移动片段得到字符串
23842385
[2341]:Problemset/2341-Maximum%20Number%20of%20Pairs%20in%20Array/README_CN.md#2341-数组能形成多少数对
23852386
[2342]:Problemset/2342-Max%20Sum%20of%20a%20Pair%20With%20Equal%20Sum%20of%20Digits/README_CN.md#2342-数位和相等数对的最大和
2387+
[2343]:Problemset/2343-Query%20Kth%20Smallest%20Trimmed%20Number/README_CN.md#2343-裁剪数字后查询第-k-小的数字
23862388
[2347]:Problemset/2347-Best%20Poker%20Hand/README_CN.md#2347-最好的扑克手牌
23872389
[2348]:Problemset/2348-Number%20of%20Zero-Filled%20Subarrays/README_CN.md#2348-全-0-子数组的数目
23882390
[2349]:Problemset/2349-Design%20a%20Number%20Container%20System/README_CN.md#2349-设计数字容器系统
@@ -3633,6 +3635,7 @@
36333635
[2337l]:https://leetcode.cn/problems/move-pieces-to-obtain-a-string/
36343636
[2341l]:https://leetcode.cn/problems/maximum-number-of-pairs-in-array/
36353637
[2342l]:https://leetcode.cn/problems/max-sum-of-a-pair-with-equal-sum-of-digits/
3638+
[2343l]:https://leetcode.cn/problems/query-kth-smallest-trimmed-number/
36363639
[2347l]:https://leetcode.cn/problems/best-poker-hand/
36373640
[2348l]:https://leetcode.cn/problems/number-of-zero-filled-subarrays/
36383641
[2349l]:https://leetcode.cn/problems/design-a-number-container-system/

0 commit comments

Comments
 (0)