-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC0478.py
27 lines (24 loc) · 870 Bytes
/
LC0478.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution(object):
def findKthNumber(self, n, k):
curr = 1
k -= 1
while k > 0:
step = self._count_steps(n, curr, curr + 1)
# If the steps are less than or equal to k, we skip this prefix's subtree
if step <= k:
# Move to the next prefix and decrease k by the number of steps we skip
curr += 1
k -= step
else:
# Move to the next level of the tree and decrement k by 1
curr *= 10
k -= 1
return curr
# To count how many numbers exist between prefix1 and prefix2
def _count_steps(self, n, prefix1, prefix2):
steps = 0
while prefix1 <= n:
steps += min(n + 1, prefix2) - prefix1
prefix1 *= 10
prefix2 *= 10
return steps