Problem:
 Score : 400 points    Problem Statement  A positive integer X is said to be a lunlun number if and only if the following condition is satisfied:   In the base ten representation of X (without leading zeros), for every pair of two adjacent digits, the absolute difference of those digits is at most 1 .   For example, 1234 , 1 , and 334 are lunlun numbers, while none of 31415 , 119 , or 13579 is.  You are given a positive integer K . Find the K -th smallest lunlun number.      Constraints   1 \leq K \leq 10^5  All values in input are integers.         Input  Input is given from Standard Input in the following format:  K       Output  Print the answer.        Sample Input 1 15      Sample Output 1 23  We will list the 15 smallest lunlun numbers in ascending order:  1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 21 , 22 , 23 . Thus, the answer is 23 .       Sample Input 2 1      Sample Output 2 1       Sample Input 3 13      Sample Output 3 21       Sample Input 4 100000      Sample Output 4 3234566667  Note that the answer may not fit into the 32 -bit signed integer type.

Solution (Python):
from collections import deque

k = int(input())
limit = 10

start = [i for i in range(1, 10)]
q = deque(start)
ans = []

while q:
    cur = q.popleft()
    if len(str(cur)) > 11:
        continue
    ans.append(cur)
    r = str(cur)[-1]
    if r == '0':
        add = ['0', '1']
    elif r == '9':
        add = ['8', '9']
    else:
        add = [str(int(r) - 1), str(int(r)), str(int(r) + 1)]

    for i in add:
        q.append(int(str(cur) + i))

ans = sorted(ans)
print(ans[k - 1])

Solution (Python):
k = int(input())
if k <= 9:
    print(k)
else:
    a = [[1,2,3,4,5,6,7,8,9]]
    cnt = 0
    anscnt = 9
    flag = 0
    while flag == 0:
        b = []
        for i in a[cnt]:
            stri = str(i)
            if stri[-1] == "0":
                now = int(stri + "0")
                b.append(now)
                anscnt += 1
                if anscnt == k:
                    print(now)
                    flag = 1
                now = int(stri + "1")
                b.append(now)
                anscnt += 1
                if anscnt == k:
                    print(now)
                    flag = 1
            elif stri[-1] == "9":
                now = int(stri + "8")
                b.append(now)
                anscnt += 1
                if anscnt == k:
                    print(now)
                    flag = 1
                now = int(stri + "9")
                b.append(now)
                anscnt += 1
                if anscnt == k:
                    print(now)
                    flag = 1
            else:
                s = int(stri[-1])-1
                now = int(stri + str(s))
                b.append(now)
                anscnt += 1
                if anscnt == k:
                    print(now)
                    flag = 1
                s += 1
                now = int(stri + str(s))
                b.append(now)
                anscnt += 1
                if anscnt == k:
                    print(now)
                    flag = 1
                s += 1
                now = int(stri + str(s))
                b.append(now)
                anscnt += 1
                if anscnt == k:
                    print(now)
                    flag = 1
            a.append(b)
            cnt += 1

Solution (Python):
import sys,math,copy,queue,itertools,bisect
from fractions import gcd
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(0,1),(-1,0),(0,-1))

k = NI()
a = [1,2,3,4,5,6,7,8,9]
cnt = 0
j = 0
while True:
    x = a[j]
    j += 1
    s = x % 10
    for i in range(s-1,s+2):
        if i >= 0 and i < 10:
            a.append(x * 10 + i)
            cnt += 1
    if cnt > 100000: break
print (a[k-1])

Solution (Python):
K = int(input())

def is_lunlun(i):
    result = -1
    n = [ord(c) -48 for c in str(i)]
    for j in range(len(n) - 1):
        if abs(n[j] - n[j + 1]) <= 1:
            continue
        if n[j] < n[j + 1]:
            for k in range(j + 1, len(n)):
                n[k] = 0
            result = int(''.join(str(k) for k in n))
            result += 10 ** (len(n) - (j + 1))
        else:
            result = int(''.join(str(k) for k in n))
            result += 10 ** (len(n) - (j + 2))
        break
    return result

i = 1
while True:
    
    t = is_lunlun(i)
    if t == -1:
        K -= 1
        if K == 0:
            print(i)
            exit()
        i += 1
    else:
        i = t

Solution (Python):
from collections import deque

K=int(input())

M=1
L=9
tmp=deque([1,2,3,4,5,6,7,8,9])
k=1
while L<K:
    k=str(tmp.popleft())
    if int(k[-1])-1>=0:
        tmp.append(int(k+str(int(k[-1])-1)))
        L+=1
    tmp.append(int(k+k[-1]))
    L+=1
    if int(k[-1])+1<10:
        tmp.append(int(k+str(int(k[-1])+1)))
        L+=1
    M+=1

print(tmp[K-M])

Solution (Python):
k=int(input())
cur=1
ans=-1
cnt=0
while cnt<k:
    num=str(cur)
    n=len(num)
    flag=True
    pos=-1
  
    for i in range(n-1):
        if abs(int(num[i])-int(num[i+1]))>1:
            pos=i+1
            flag=False
            break
 
    if flag==True:
        ans=cur
        cur+=1
        cnt+=1
    else:
        if int(num[pos])!=9:
            ch=str(int(num[pos])+1)

            cur=int(num[:pos]+ch+num[pos+1:])
        else:
            ch=str(int(num[pos-1])+1)
            cur=int(num[:pos-1]+ch+'0'*len(num[pos:]))
print(ans)

Solution (Python):


import math
K = int(input())
Ln = [[]]
Ln[0] = [1 for i in range(10)]
for i in range(1,10):
    tmp =[Ln[i-1][0] + Ln[i-1][1]] + [Ln[i-1][j-1] + Ln[i-1][j] + Ln[i-1][j+1] for j in range(1,9)] +[Ln[i-1][8] + Ln[i-1][9] ]
    Ln.append(tmp)


X = K
Y = 0
fin =(-1,-1)
flag = 1
for i in range(10):
    for j in range(1,10):
        Y = X
        X -= Ln[i][j]

        if X<=0 : 
            fin = (i,j)
            flag = 0
            break
        
    if flag == 0:
        break

ans = 10**i * j 

p=i

for i in range(p-1,-1,-1):
    X = Y
    flag = 1

    if j==0 :
        lis = [0,1]
    elif j==9:
        lis = [8,9]
    else:
        lis = [j-1,j,j+1]
    for j in lis:
        Y = X
        X -= Ln[i][j]

        if X<=0:
            ans += 10**i * j

            flag = 0
        if flag == 0:
            break
print(ans)

Solution (Python):
k = int(input())

run = [1,2,3,4,5,6,7,8,9]

val = ['' for i in range(10)]

kouho = []



def dfs(pos,keta,num):
    if keta == pos:
        
        val[pos] = num
        
        tmp = 0
        for i in range(keta+1):
            
            tmp += val[i] * 10 ** (keta-i)
            
        
        kouho.append(tmp)
        """
        if num == k:
            print(tmp)
            exit()
            
        """
        
        return
    
    for i in range(3):
        
        val[pos] = num
        
        if num-1+i > 9:
            continue
        if num-1+i < 0:
            continue
        dfs(pos+1,keta,num-1+i)
    
for i in range(10):
    for j in range(1,10):
        
        dfs(0,i,j)


print(kouho[k-1])

Solution (Python):

MOD = 10 ** 9 + 7
INFI = 10**10

import math

import itertools
import heapq


import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default




def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)


def main():
    k=int(input())
    keta1=[x for x in range(1,10)]
    oriketa=copy.deepcopy(keta1)
    def next(keta,newketa):
        for i in range(len(keta)):
            plus=keta[i]%10+1
            eq=keta[i]%10
            minus=keta[i]%10-1
            if plus<=9:
                newketa.append(keta[i]*10+plus)
            newketa.append(keta[i]*10+eq)
            if minus>=0:
                newketa.append(keta[i]*10+minus)
        return
    ketan=[[] for i in range(10)]
    next(keta1,ketan[0])
    ketan[0]=sorted(ketan[0])
    oriketa.extend(ketan[0])
    for i in range(9):
        next(ketan[i],ketan[i+1])
        ketan[i+1] = sorted(ketan[i+1])
        oriketa.extend(ketan[i+1])

    
    print(oriketa[k-1])



if __name__ == "__main__":

    main()

Solution (Python):
def calc(K):
    l = [i for i in range(1, 10)]
    n = 9
    while True:
        if n >= K:
            return l[K - 1]
        K -= n
        newl = []
        n = 0
        for x in l:
            d = x % 10
            newl += [x * 10 + y for y in [d - 1, d, d + 1] if y > -1 and y < 10]
            n += (2 if (d == 0 or d == 9) else 3)
            if n >= K:
                return newl[K - 1]
        l = newl


K = int(input())
print(calc(K))

Solution (Python):
from collections import deque

from heapq import heappush, heappop


def main():
    K = int(input())

    q = []

    for i in range(1, 10):
        heappush(q, i)

    while True:
        v = heappop(q)
        K -= 1
        if K == 0:
            print(v)
            exit()
        u = v % 10
        nv = v * 10 + u

        heappush(q, nv)
        if u == 0:
            heappush(q, nv + 1)
        elif u == 9:
            heappush(q, nv - 1)
        else:
            heappush(q, nv - 1)
            heappush(q, nv + 1)


main()

Solution (Python):
import sys
input=lambda: sys.stdin.readline().rstrip()
k=int(input())
cur=0
for i in range(k):
  s=str(cur)
  n=len(s)
  if n<=1 or cur==10**n-1:
    cur+=1
  elif int(s[n-2])>=int(s[n-1]) and int(s[n-1])!=9:
    cur+=1
  elif int(s[n-2])>=int(s[n-1]) and int(s[n-1])==9:
    for j in range(n-1):
      if int(s[n-j-1])==9 or int(s[n-j-1])==int(s[n-j-2])+1:
        continue
      else:
        nn=j
        ret=s[:n-1-nn]
        ret+=str(int(s[n-1-nn])+1)
        break
    else:
      ret=str(int(s[0])+1)
      nn=n-1
    
    for j in range(nn):
      ret+=str(max(int(ret[-1])-1,0))
    cur=int(ret)
  else: 
    for j in range(1,n-1):
      if int(s[n-1-j-1])>=int(s[n-1-j]):
        nn=n-1-j
        ret=s[:nn]
        ret+=str(int(s[nn])+1)
        break
    else:
      nn=0
      ret=str(int(s[0])+1)
    for j in range(n-nn-1):
      ret+=str(max(int(ret[-1])-1,0))
    cur=int(ret)
print(cur)

Solution (Python):
lunlunnum = []
def lunlun(n):
    if n >= 10000000000:
        return
    lunlunnum.append(n)
    d = n%10
    if d == 0:
        next_d = [0, 1]
    elif d == 9:
        next_d = [8, 9]
    else:
        next_d = [d-1, d, d+1]
    for i in next_d:
        lunlun(n*10 + i)
for i in range(1, 10):
    lunlun(i)
lunlunnum.sort()
k = int(input())
print(lunlunnum[k-1])

Solution (Python):
K = int(input())

num_table = [
	[1, 2, 3, 4, 5, 6, 7, 8, 9]
]

while True:
	if K < len(num_table[-1]):



		print(num_table[-1][K - 1])
		break
	K -= len(num_table[-1])
	

	d = 1
	d2 = 10 ** len(num_table)
	new_num_table = set((d2,))
	for i in range(0, len(num_table) - 1):
		for num in num_table[i]:
			if num // d == 1:
				new_num_table.add(d2 + num)
		d *= 10
	
	for num in num_table[-1]:
		top = num // d
		if top > 1:
			new_num_table.add((top - 1) * d2 + num)
		if top > 0:
			new_num_table.add(top * d2 + num)
		if top < 9:
			new_num_table.add((top + 1) * d2 + num)
	
	new_num_table = list(new_num_table)
	new_num_table.sort()
	
	num_table.append(new_num_table)

Solution (Python):







def lunlun_gen(which):
    R = []

    D = [1]
    while len(R) < which:
        num = 0
        for d in D:
            num = 10*num + d
        R.append(num)

        for i in reversed(range(1, len(D))):
            if D[i] + 1 <= 9 and abs(D[i] + 1 - D[i-1]) <= 1:
                D[i] += 1
                break
        else:
            
            i = 0
            if D[0] == 9:
                D.append(0)
                D[0] = 1
            else:
                D[0] += 1

                
        for j in range(i+1, len(D)):
            D[j] = max(0, D[j-1] - 1)
    return R[which-1]

k = int(input())
print(lunlun_gen(k))

Solution (Python):
K=int(input())
if K<10:
    n=2
    i=1
else:
    n=10
    i=9
while i<K:
    S=str(n)
    cond=True
    for j in range(len(S)-1):
        if abs(int(S[j])-int(S[j+1]))>1:
            cond=False
            break
    if cond:
        i+=1

        n+=1
    else:
        n+=10**(len(S)-j-2)

print(n-1)

Solution (Python):
import sys

sys.setrecursionlimit(100010)
k = int(input())


def islunlun(l):
    for x in range(1,len(l)):
        if abs(ord(l[x])-ord(l[x-1])) > 1:
            return False
    return True

s = set()
mx = 3234566667
def generate(l):
    val = None
    if l == []:
        val = 0
    else:
        val = int("".join(list(map(str,l))))
    
    if mx < val:
        return
    else:
        if l != []:
            s.add(val)
    if len(l) == 0:
        for x in range(1,10):
            l.append(x)
            generate(l.copy())
            l.pop(len(l)-1)
    else:
        
        for x in range(max(0,l[len(l)-1]-1), min(9,l[len(l)-1]+1)+1):
            l.append(x)
            generate(l)
            l.pop(len(l)-1)

"""def bt(l):
    for x in range(10):
        l.append()"""

generate([])
l = []
for x in s:
    l.append(x)
l.sort()
print(l[k-1])

Solution (Python):
from collections import deque

K = int(input())
if K < 10:
    print(K)
else:
    K -= 9
    q = deque(list(range(1, 10)))
    while K:
        num = q.popleft()
        last_digit = str(num)[-1]
        if last_digit == '0':
            q.append(int(str(num) + '0')); K -= 1
            if K == 0: break
            q.append(int(str(num) + '1')); K -= 1
        elif last_digit == '9':
            q.append(int(str(num) + '8')); K -= 1
            if K == 0: break
            q.append(int(str(num) + '9')); K -= 1
        else:
            q.append(int(str(num) + str(int(last_digit) - 1))); K -= 1
            if K == 0: break
            q.append(int(str(num) + str(int(last_digit)))); K -= 1
            if K == 0: break
            q.append(int(str(num) + str(int(last_digit) + 1))); K -= 1
    print(q[-1])

Solution (Python):
K = int(input())

def runrun_build(K):
    runrun_list = list(range(1, 10))
    base_list = runrun_list.copy()
    cnt = 9
    for i in range(0, 9):
        new_base_list = []
        for val in base_list:
            val_list = []
            if val % 10 == 0:
                for j in range(0, 2):
                    val_list.append(val * 10 + val % 10 + j)
                    cnt += 1
            elif val % 10 == 9:
                for j in range(-1, 1):
                    val_list.append(val * 10 + val % 10 + j)
                    cnt += 1
            else:
                for j in range(-1, 2):
                    val_list.append(val * 10 + val % 10 + j)
                    cnt += 1
            new_base_list.extend(val_list)
            if cnt > K:
                runrun_list.extend(new_base_list)
                return runrun_list
        runrun_list.extend(new_base_list)
        base_list = new_base_list
    return runrun_list

print(sorted(runrun_build(K))[K-1])

Solution (Python):
from collections import deque


def lunlun(k: int):
    q = deque([str(i) for i in range(1, 10)])
    res = '1'

    while k > 0:
        res = q.popleft()
        left = int(res[-1]) - 1
        mid = int(res[-1])
        right = int(res[-1]) + 1

        if left >= 0:
            q.append("{}{}".format(res, str(left)))
        q.append("{}{}".format(res, str(mid)))
        if right <= 9:
            q.append("{}{}".format(res, str(right)))

        k -= 1

    return int(res)


if __name__ == '__main__':
    k = int(input())
    print(lunlun(k))

Solution (Python):
import sys


sys.setrecursionlimit(10 ** 9)


k = int(input())

ary = [1, 2, 3, 4, 5, 6, 7, 8, 9]

def ans(j, nums):
    nums[j] += 1
    pre = nums[j]
    for i in range(j + 1, len(nums)):
        if pre > 0:
            nums[i] = pre - 1
            pre = nums[i]
        else:
            nums[i] = pre
    return int(''.join(map(str, nums)))

def lunlun(n):
    if n <= 9:
        return ary[n - 1]
    l = lunlun(n - 1)
    nums = list(map(int, list(str(l))))
    first = len(nums) - 1
    last = 0
    for i in range(len(nums)):
        j = len(nums) - i - 1
        c = nums[j] + 1
        
        if j == first:
            
            if nums[j] < 9 and abs(nums[j - 1] - c) <= 1:
                nums[j] += 1
                return int(''.join(map(str, nums)))
        elif j == last:
            
            if nums[j] < 9 and abs(nums[j + 1] - c) <= 1:
                return ans(j, nums)
        elif j != first and j != last:
            
            if nums[j] < 9 and abs(nums[j - 1] - c) <= 1 and abs(nums[j + 1] - c) <= 1:
                return ans(j, nums)

    
    return int('1' + ''.join(['0'] * len(nums)))

print(lunlun(k))

Solution (Python):
import sys
import heapq

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	k = II()
	a = [1,2,3,4,5,6,7,8,9]
	heapq.heapify(a)
	total = 0
	while(total!=k):
		i = heapq.heappop(a)
		total+=1
		li = i%10
		if(li%10==0):
			heapq.heappush(a,i*10+li)
			heapq.heappush(a,i*10+li+1)
		elif(li%10==9):
			heapq.heappush(a,i*10+li-1)
			heapq.heappush(a,i*10+li)
		else:
			heapq.heappush(a,i*10+li-1)
			heapq.heappush(a,i*10+li)
			heapq.heappush(a,i*10+li+1)
	print(i)

if __name__ == '__main__':
	main()

Solution (Python):
k = int(input())
ans = [-1] * 11
ans[-1] = 1
idx = 1
def add(keta):
    if ans[keta] == -1:
        ans[keta] = 1
    else:
        ans[keta] += 1
        if ans[keta] == 10:
            add(keta - 1)
            ans[keta]=max(0,ans[keta-1]-1)
        elif abs(ans[keta - 1] - ans[keta]) > 1 and ans[keta - 1] != -1:
            add(keta - 1)
            ans[keta] = max(0, ans[keta - 1] - 1)
        
while idx != k:
    idx+=1
    if ans[-2] == -1:
        if ans[-1] == 9:
            ans[-2] = 1
            ans[-1] = 0
        else:
            ans[-1] += 1
        continue
    add(-1)

ans=[ans[i] for i in range(len(ans)) if ans[i]!=-1]
ans=map(str,ans)
print("".join(ans))

Solution (Python):

from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product





K = int(input())


l_1 = [] 

num_list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
index_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

for i in range(1, len(num_list)):
    l_1.append(num_list[i])



def plus_keta(l):
    
    list_return = []
    for i in range(len(l)):
        last_number = l[i][-1]
        
        if(last_number == '0'):
            list_return.append(l[i] + num_list[0])
            list_return.append(l[i] + num_list[1])
        elif(last_number == '9'):
            list_return.append(l[i] + num_list[8])
            list_return.append(l[i] + num_list[9])
        else:
            last_number_index = eval(last_number)
            list_return.append(l[i] + num_list[last_number_index-1])
            list_return.append(l[i] + num_list[last_number_index])
            list_return.append(l[i] + num_list[last_number_index+1])
    return list_return




l_2 = plus_keta(l_1)

l_3 = plus_keta(l_2)

l_4 = plus_keta(l_3)

l_5 = plus_keta(l_4)

l_6 = plus_keta(l_5)

l_7 = plus_keta(l_6)

l_8 = plus_keta(l_7)

l_9 = plus_keta(l_8)

l_10 = plus_keta(l_9)


l = []
l.extend(l_1)
l.extend(l_2)
l.extend(l_3)
l.extend(l_4)
l.extend(l_5)
l.extend(l_6)
l.extend(l_7)
l.extend(l_8)
l.extend(l_9)
l.extend(l_10)


print(int(l[K-1]))


'''
s = ''
for i in range(len(n)):
    s += n[i]
print(s)
print(eval(s) - 3)
''' 
'''
def gen_lun(n):
    
    
    for i in range(1, len(num_list)):
        print(i)
'''

Solution (Python):
def inc(t):
	n=len(t)
	if n==1:
		if t[0] <9:
			t[0]+=1
		else : t=[1,0]
		return t
	if all(t[i]==9 for i in range(n)):
		t = [1]
		t.extend([0]*n)
		return t
	for i in range(n-1,-1,-1):
		if i==0 or t[i]-t[i-1]<1:
			if t[i]<9:
				t[i]+=1
				for j in range(i+1,n):
					t[j] = max(0,t[j-1]-1)
				return t
			
	

k=int(input())
t = [0]

for i in range(k):
	t = inc(t)
print("".join([str(i) for i in t]))

Solution (Python):
K=int(input())
A = [[] for j in [0]*(10**5)]
A[0]=[1,2,3,4,5,6,7,8,9]
B=9
C=0
while B<=K:
 for i in range(len(A[C])):
  if A[C][i]%10>0:
   A[C+1].append(A[C][i]*10+(A[C][i]%10)-1)
  A[C+1].append(A[C][i]*10+(A[C][i]%10))
  if A[C][i]%10<9:
   A[C+1].append(A[C][i]*10+(A[C][i]%10)+1)
 B+=len(A[C+1])
 C+=1
print(A[C][len(A[C])-B+K-1])

Solution (Python):
from collections import deque

K = int(input())

if K <= 9:
    print(str(K))
else:
    tmp = deque([1, 2, 3, 4, 5, 6, 7, 8, 9])
    k = 9
    while k < K:
        cur = tmp.popleft()
        ones_place = int(str(cur)[-1])
        if ones_place == 0:
            tmp.append(int(str(cur)+str(0)))
            tmp.append(int(str(cur)+str(1)))
            k += 2
        elif ones_place == 9:
            tmp.append(int(str(cur)+str(8)))
            tmp.append(int(str(cur)+str(9)))
            k += 2
        else:
            tmp.append(int(str(cur)+str(ones_place - 1)))
            tmp.append(int(str(cur)+str(ones_place)))
            tmp.append(int(str(cur)+str(ones_place+1)))
            k += 3

    print(str(tmp[-(k-K)-1]))

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    

    def dump(*args):
        sys.stderr.write(str(args))

    k = int(input())

    def isOk(n):
        n = str(n)
        for i in range(len(n)):
            if (i+1 < len(n) and abs(int(n[i]) - int(n[i + 1])) > 1 or i+2 < len(n) and abs(int(n[i + 1]) - int(n[i + 2])) > 1):
                return False
        return True

    def swap(n, i, f):
        ori = n
        ns = str(n)
        try:
            t = ns[-i-1]
            w = ns[-i - 2]
        except:
            s = int(f) + 1
            l = len(ns)
            if s == 10:
                l += 1
            a = ''
            for i in range(s, -1, -1):
                a += str(i)
            a += '0'*(l-len(a))
            return int(a[:l])
        if t == w:
            return swap(n, i+1, f)
        n -= int(t) * 10 ** i
        n -= int(w)*10**(i+1)
        n += int(t) * 10 ** (i + 1)
        n += int(w) * 10 ** (i)
        l = len(ns)
        a = str(n)[:-i-1]
        for i in range(int(a[-1])-1, -1, -1):
            a += str(i)
        a = a[:l]
        a += '0'*(l-len(a))
        n = int(a[:l])
        if not isOk(n):
            return swap(ori, i+1, f)
        return n

    def calc(n, I):
        if isOk(n):
            return n
        n -= 10**I
        n += 10 ** (I + 1)
        a = str(n)[: -I - 1]
        l = len(str(n))
        for i in range(int(a[-1])-1, -1, -1):
            a += str(i)
        a = a[:l]
        a += '0'*(l-len(a))
        n = int(a[:l])
        return calc(n, I+1)

    dp = [0] * (k+1)
    ind = 0
    for i in range(k):
        
            
        dp[i + 1] = calc(dp[i] + 1, 0)
        
        

        
        
        
        
        
    print(dp[k])


if __name__ == '__main__':
    main()

Solution (Python):
k=int(input())
l=["1","2","3","4","5","6","7","8","9"]
for i in range(k):
    x=l[i]
    y=int(x[-1])
    if y==0:
        l.append(x+str(y))
        l.append(x+str(y+1))
    elif y==9:
        l.append(x+str(y-1))
        l.append(x+str(y))
    else:
        l.append(x+str(y-1))
        l.append(x+str(y))
        l.append(x+str(y+1))
print(l[k-1])

Solution (Python):
K = int(input())

d = [[] for i in range(12)]

for i in range(1, 10):
    d[1].append(i)

if K <= 9:
    print(K)
    exit()

cur = K - 9

for i in range(2, 12):
    for t in d[i - 1]:
        r = t % 10
        if r == 0:
            d[i].append(t * 10)
            d[i].append(t * 10 + 1)
        elif r == 9:
            d[i].append(t * 10 + 9)
            d[i].append(t * 10 + 8)
        else:
            d[i].append(t * 10 + r - 1)
            d[i].append(t * 10 + r)
            d[i].append(t * 10 + r + 1)

    d[i].sort()

    if cur <= len(d[i]):
        print(d[i][cur - 1])
        exit()

    cur -= len(d[i])

Solution (Python):
import math

K = int(input())

def develop_lun(lun):
  lastdigit = lun % 10
  if lastdigit==0:
    return [lun*10+lastdigit, lun*10+lastdigit+1]
  elif lastdigit==9:
    return [lun*10+lastdigit-1, lun*10+lastdigit]
  else:
    return [lun*10+lastdigit-1, lun*10+lastdigit, lun*10+lastdigit+1]

lunlist = [1,2,3,4,5,6,7,8,9]
i = 0
while len(lunlist)<K:
  lunlist.extend(develop_lun(lunlist[i]))
  i+=1

print(lunlist[K-1])

Solution (Python):
k = int(input())

def lunlun(x):
    s = str(x)
    good = 0
    for i in range(1, len(s)):
        if abs(int(s[i - 1]) - int(s[i])) > 1:
            
            return 10 ** (len(s) - i - 1)
    return good

n = 1
last = 0
total = 0
while total < k:
    skip = lunlun(n)
    if skip == 0:
        last = n
        total += 1
        n += 1
    else:
        n += skip

print(last)

Solution (Python):
K = int(input())

if K < 10:
    print(K)
else:
    l, temp1, temp2, i, j = [[1]*10], K, 9, 1, 0
    while temp1 > temp2:
        temp1 -= temp2
        if j == 0:
            l.append([0]*10)
            l[i][j] = l[i-1][0] + l[i-1][1]
            temp2 = 0
            j += 1
        elif j == 9:
            l[i][j] = l[i-1][8] + l[i-1][9]
            temp2 = l[i][j]
            i += 1
            j = 0
        else:
            l[i][j] = l[i-1][j-1] + l[i-1][j] + l[i-1][j+1]
            temp2 = l[i][j]
            j += 1

    ans, p = j - 1, j - 1
    K = temp1
    for k in range(i)[::-1]:
        if p == 0:
            if K > l[k][0]:
                p, K = 1, K - l[k][0]
            else:
                p, K = 0, K
        elif p == 9:
            if K > l[k][8]:
                p, K = 9, K - l[k][8]
            else:
                p, K = 8, K
        else:
            if K > l[k][p-1] + l[k][p]:
                p, K = p+1, K - (l[k][p-1] + l[k][p])
            elif K > l[k][p-1]:
                p, K = p, K - l[k][p-1]
            else:
                p, K = p-1, K
        ans = ans*10 + p

    print(ans)

Solution (Python):
k = int(input())
l = set()


def construct(lunnum: str, depth: int, flag: bool):
    global l
    if depth == 0:
        l.add(int(lunnum))
        return
    m = int(lunnum[-1])
    for i in range(0, 10) if flag else set([max(0, m - 1), m, min(9, m + 1)]):
        construct(lunnum + str(i), depth - 1, (i == 0) & flag)


construct("0", 10, True)
ls = sorted(list(l))
print(ls[k])

Solution (Python):
k = int(input())
l = set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
l1 = [[[] for i in range(10)] for i in range(10)]
for i in range(10):
  l1[0][i].append(i)
for i in range(1, 10):
  for j in range(0, 10):
    if j == 0:
      l1[i][j] = l1[i-1][0] + l1[i-1][1]
    elif j == 1:
      l1[i][j] = l1[i-1][0] + l1[i-1][1] + l1[i-1][2]
    elif j == 2:
      l1[i][j] = l1[i-1][3] + l1[i-1][1] + l1[i-1][2]
    elif j == 3:
      l1[i][j] = l1[i-1][3] + l1[i-1][4] + l1[i-1][2]
    elif j == 4:
      l1[i][j] = l1[i-1][3] + l1[i-1][4] + l1[i-1][5]
    elif j == 5:
      l1[i][j] = l1[i-1][6] + l1[i-1][5] + l1[i-1][4]
    elif j == 6:
      l1[i][j] = l1[i-1][7] + l1[i-1][6] + l1[i-1][5]
    elif j == 7:
      l1[i][j] = l1[i-1][8] + l1[i-1][7] + l1[i-1][6]
    elif j == 8:
      l1[i][j] = l1[i-1][9] + l1[i-1][8] + l1[i-1][7]
    else:
      l1[i][j] = l1[i-1][9] + l1[i-1][8]
    l1[i][j] = [x + j * 10**i for x in l1[i][j]]
    l = l | set(l1[i][j])
l2 = list(l)
l2.sort()
print(l2[k])

Solution (Python):
k = int(input())

def forndigit(l1):
    l = []
    for i in l1:
        if i[-1] == '1':
            l.append(i+'0')
            l.append(i+'1')
            l.append(i+'2')
        elif i[-1] == '2':
            l.append(i+'1')
            l.append(i+'2')
            l.append(i+'3')
        elif i[-1] == '3':
            l.append(i+'2')
            l.append(i+'3')
            l.append(i+'4')
        elif i[-1] == '4':
            l.append(i+'3')
            l.append(i+'4')
            l.append(i+'5')
        elif i[-1] == '5':
            l.append(i+'4')
            l.append(i+'5')
            l.append(i+'6')
        elif i[-1] == '6':
            l.append(i+'5')
            l.append(i+'6')
            l.append(i+'7')
        elif i[-1] == '7':
            l.append(i+'6')
            l.append(i+'7')
            l.append(i+'8')
        elif i[-1] == '8':
            l.append(i+'7')
            l.append(i+'8')
            l.append(i+'9')
        elif i[-1] == '9':
            l.append(i+'8')
            l.append(i+'9')
        elif i[-1] == '0':
            l.append(i+'0')
            l.append(i+'1')
    return l

l1 = [str(i) for i in range(1,10)]
r = [l1]
for i in range(2,11):
    r.append(forndigit(r[i-2]))
ans = []
for item in r:
    ans += item
print(ans[k-1])

Solution (Python):
d = int(input())

lists = [1, 2, 3, 4, 5, 6, 7, 8, 9]

if d < 10:
    print(d)
    exit()

times = 9

while True:
    tmps = []
    for i in lists:  
        if i % 10 == 0:
            times += 2
            tmps.append(i * 10)
            tmps.append(i * 10 + 1)
        elif i % 10 == 9:
            times += 2
            tmps.append(i * 10 + 8)
            tmps.append(i * 10 + 9)
        else:
            times += 3
            tmps.append(i * 10 + (i % 10 - 1))
            tmps.append(i * 10 + (i % 10))
            tmps.append(i * 10 + (i % 10 + 1))
        
        if times >= d:
            print(tmps[len(tmps) - abs(times - d) - 1])
            exit()
            
    lists = tmps

Solution (Python):
code = '9876543210000000000'
def judge(n):
    n = str(n)
    for i in range(len(n)-1):
        ni = int(n[i])
        if abs((ni) - int(n[i+1])) > 1:
            if ni==9:

                return int(n[:i]+code[:len(n)-i])
            else:
                idx = 8 - ni
                return int(n[:i]+code[idx:idx+len(n)-i])
    return -1


i = 1
cnt = 0
k = int(input())
while(1):
    j = judge(i)
    if j==-1:
        cnt += 1

        i+=1
    else:
        i = j
    if cnt == k:
        print(i-1)
        exit()