-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKthSmallestInLexicographicalOrder.py
More file actions
50 lines (40 loc) · 1.22 KB
/
KthSmallestInLexicographicalOrder.py
File metadata and controls
50 lines (40 loc) · 1.22 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n.
Note: 1 <= k <= n <= 109.
Example:
Input:
n: 13 k: 2
Output:
10
Explanation:
The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so the second smallest number is 10.
"""
import math
class Solution(object):
def findKthNumber(self, n, k):
"""
:type n: int
:type k: int
:rtype: int
"""
def find(n, k, m):
from math import log10
print n
l = int(log10(n - 1))
base = int('1'*(l+1))
p, q = n / 10 ** l, n % (10 ** l)
s = [base] * (p) + [q+ base / 10 + 1] + [base / 10] * (9 - p)
t = [10**l] * (p) + [q + 1] + [10 ** (l-1)] * (9 - p)
first = m
print s, n, k
while(k > s[first]):
k -= s[first]
first += 1
print first, k
if k > 1:
return str(first) + str(find(t[first] - 1, k - 1, 0))
else:
return str(first)
return int(find(n, k , 1))
def generateKthNumber(self, n, k):
return sorted(range(1,n+1), key = lambda x: str(x))[k-1]