This commit is contained in:
parent
9d128a5f16
commit
b96b438b8a
1 changed files with 24 additions and 0 deletions
24
hard/k_th_smallest_in_lexicographical_order.py
Normal file
24
hard/k_th_smallest_in_lexicographical_order.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# https://leetcode.com/problems/k-th-smallest-in-lexicographical-order
|
||||
|
||||
class Solution:
|
||||
def findKthNumber(self, n: int, k: int) -> int:
|
||||
def max_step(curr: int) -> int:
|
||||
step = 0
|
||||
first, last = curr, curr
|
||||
while first <= n:
|
||||
step += min(n, last) - first + 1
|
||||
first *= 10
|
||||
last = last * 10 + 9
|
||||
return step
|
||||
curr = 1
|
||||
k -= 1
|
||||
while k > 0:
|
||||
step = max_step(curr)
|
||||
if step <= k:
|
||||
k -= step
|
||||
curr += 1
|
||||
else:
|
||||
k -= 1
|
||||
curr *= 10
|
||||
return curr
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue