-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path440.py
More file actions
36 lines (36 loc) · 1.24 KB
/
440.py
File metadata and controls
36 lines (36 loc) · 1.24 KB
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
28
29
30
31
32
33
34
35
36
__________________________________________________________________________________________________
sample 28 ms submission
class Solution:
def findKthNumber(self, n, k):
k -= 1
cur = 1
while k > 0:
step, first, last = 0, cur, cur + 1
while first <= n:
step += min(n + 1, last) - first
first *= 10
last *= 10
if step <= k:
cur += 1
k -= step
else:
cur *= 10
k -= 1
return cur
__________________________________________________________________________________________________
sample 32 ms submission
class Solution:
def findKthNumber(self, n: int, k: int) -> int:
def getSteps(cur):
steps, n1, n2 = 0, cur, cur + 1
while n1 <= n:
steps += min(n + 1, n2) - n1
n1, n2 = n1 * 10, n2 * 10
return steps
cur, k = 1, k - 1
while k > 0:
steps = getSteps(cur)
if steps <= k: k, cur = k - steps, cur + 1
else: cur, k = cur * 10, k - 1
return cur
__________________________________________________________________________________________________