Skip to content

Commit 22b3aab

Browse files
authored
Update 440-k-th-smallest-in-lexicographical-order.js
1 parent 09b9a79 commit 22b3aab

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

440-k-th-smallest-in-lexicographical-order.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,38 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const findKthNumber = function (n, k) {
7+
let cur = 1
8+
k = k - 1
9+
while(k > 0) {
10+
const num = calc(cur)
11+
if(num <= k) {
12+
cur++
13+
k -= num
14+
} else {
15+
k--
16+
cur *= 10
17+
}
18+
}
19+
return cur
20+
21+
function calc(cur) {
22+
let total = 0
23+
let nxt = cur + 1
24+
while(cur <= n) {
25+
total += Math.min(n - cur + 1, nxt - cur)
26+
nxt *= 10
27+
cur *= 10
28+
}
29+
30+
return total
31+
}
32+
}
33+
34+
// another
35+
136
/**
237
* @param {number} n
338
* @param {number} k

0 commit comments

Comments
 (0)