Article directory
topic
Leetcode 440. The Kth decimal number in dictionary order
Code (10.2 first brush to see the analysis)
The priority queue for the first flush has timed out.
Lexicographic order
class Solution {
public:
int findKthNumber(int n, int k) {
int curr = 1;
k--;
while(k) {
int steps = getSteps(curr, n);
if(steps <= k) {
k -= steps;
curr++;
} else {
curr = curr*10;
k--;
}
}
return curr;
}
int getSteps(int curr, long n) {
int steps = 0;
long first = curr, last = curr;
while(first <= n) {
steps += min(last, n) - first + 1;
first = first * 10;
last = last * 10 + 9;
}
return steps;
}
};