-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #988 from 0xff-dev/2343
Add solution and test-cases for problem 2343
- Loading branch information
Showing
3 changed files
with
90 additions
and
27 deletions.
There are no files selected for viewing
44 changes: 30 additions & 14 deletions
44
leetcode/2301-2400/2343.Query-Kth-Smallest-Trimmed-Number/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 49 additions & 2 deletions
51
leetcode/2301-2400/2343.Query-Kth-Smallest-Trimmed-Number/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters