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.



Sample input:
15


Sample output:
23

Submission status: Time Limit Exceeded

Solution code:
que=[1,2,3,4,5,6,7,8,9]
k=int(input())
for i in range(k-1):
  q=que.pop(0)
  #print("q",q)
  m=q%10
  #print("m",m,"que",que)
  if m==0:
    que.append(q*10)
    que.append(q*10+1)
  elif m==9:
    que.append(q*10+8)
    que.append(q*10+9)
  else:
    que.append(q*10+m-1)
    que.append(q*10+m)
    que.append(q*10+m+1)
print(que[0])

Submission status: Time Limit Exceeded

Solution code:
k = int(input())
lis = []
num = 0
i = 0
while num < k:
    i += 1
    a = list(map(int, list(str(i))))
    j = 0
    tf = True
    while j < len(a)-1:
        if abs(a[j]-a[j+1]) > 1:
            tf = False
            break
        j += 1
    if tf == True:
        num += 1
print(i)

Submission status: Time Limit Exceeded

Solution code:
K = int(input())
Q = [1,2,3,4,5,6,7,8,9]
for i in range(K):
    x = Q.pop(0)
    if x%10!=0:
        Q.append(10*x + x%10 - 1)
    Q.append(10*x + x%10)
    if x%10!=9:
        Q.append(10*x + x%10 + 1)
print(x)

Submission status: Time Limit Exceeded

Solution code:
# ABC161D
def f(k):
    if 1 <= k <= 9:
        print(k)
    nxt = [[0,1], [0,1,2], [1,2,3], [2,3,4], [3,4,5], [4,5,6], [5,6,7], [6,7,8], [7,8,9], [8,9]]
    cum_npi = [0, 9, 35, 110, 327, 956, 2782, 8089, 23527, 68468, 199368]
    for length, begin in enumerate(cum_npi):
        if k < begin:
            found = cum_npi[length - 1]
            break
    begin = 10 ** (length - 1)
    length, begin, found
    k -= found
    x = [[]] * (length + 1)
    x[1] = list(range(1, 10))
    x

    terminate = False
    i = 1
    while True:
        #print(f"a) i = {i}  x = {x}")
        if len(x[i]): # len(x[i])
            digit = x[i][0]
            if i < length: # i, length
                i += 1
                x[i] = nxt[digit].copy()
                #print(f"x) digit = {digit}  nxt[digit] = {nxt[digit]}  i = {i}  x[i] = {x[i]}")
                #print(f"b) i = {i}  x = {x}")
                if i == length: # i, length
                    while len(x[i]): # i, x[i]
                        k -= 1
                        #print(f"c) i = {i}  x = {x}, k = {k}")
                        if k == 0:
                            x.pop(0)
                            #print("answer =", x)
                            ans = "".join([str(j.pop(0)) for j in x])
                            print(ans)
                            terminate = True
                            break
                        else:
                            x[i].pop(0)
                        #print(f"d) i = {i}  x = {x}")
                    else:
                        #print(f"e) i = {i}  x = {x}")
                        while len(x[i]) == 0:
                            i -= 1
                            x[i].pop(0)
                    if terminate:
                        break

f(int(input()))

Submission status: Time Limit Exceeded

Solution code:
K = int(input())
queue = []
for i in range(1, 10):
  queue.append(i)

while K != 0:
  num = queue.pop(0)
  K -= 1

  if K == 0:
    print(num)
    exit(0)

  o = int(str(num)[-1])
  for n in [o-1, o, o+1]:
    if n == -1 or n == 10: continue
    queue.append( int(str(num)+str(n)) )

Submission status: Time Limit Exceeded

Solution code:
k=int(input())
l=list(range(1,10))
def sousa():
    global l
    x=l[0]
    l.pop(0)
    if x%10!=0:
        l.append(10*x+x%10-1)
    l.append(10*x+x%10)
    if x%10!=9:
        l.append(10*x+x%10+1)
    return x

for i in range(k):
    k=sousa()
print(k)

Submission status: Time Limit Exceeded

Solution code:
K = int(input())

k = 0
nums = [str(n) for n in range(1, 10)]
while K != k:
    num = nums.pop(0)
    last1 = num[len(num) - 1]
    if last1== "0":
        append_num = [num + str(n) for n in [0, 1]]
    elif last1 == "9":
        append_num = [num + str(n) for n in [8, 9]]
    else:
        append_num = [num + str(n) for n in [int(last1) - 1, int(last1), int(last1) + 1]]
    nums += append_num
    k += 1
else:
    print(int(num))

Submission status: Time Limit Exceeded

Solution code:
K = int(input())

k = 0
nums = [n for n in range(1, 10)]
while K != k:
    num = nums.pop(0)
    str_num = str(num)
    length = len(str_num)
    if str_num[length - 1] == "0":
        append_num = [str_num + str(n) for n in [0, 1]]
    elif str_num[length - 1] == "9":
        append_num = [str_num + str(n) for n in [8, 9]]
    else:
        append_num = [str_num + str(n) for n in range(int(str_num[length - 1]) - 1, int(str_num[length - 1]) + 2)]
    nums += append_num
    k += 1
else:
    print(num)

Submission status: Time Limit Exceeded

Solution code:
K = int(input())
queue = [1, 2, 3, 4, 5, 6, 7, 8, 9]

for i in range(K):
  x = queue.pop(0)
  
  if x % 10 != 0:
    queue.append(10 * x + (x % 10) - 1)
    
  queue.append((10 * x) + x % 10)
    
  if x % 10 != 9:
    queue.append(10 * x + (x % 10) + 1)
    
print(x)

Submission status: Time Limit Exceeded

Solution code:
# wrong answer
K=int(input())
A=[1,2,3,4,5,6,7,8,9]
for _ in range(K-1):
    n=A.pop(0)
    if n%10!=0: A.append(10*n+(n%10-1))
    A.append(10*n+n%10)
    if n%10!=9: A.append(10*n+(n%10+1))
print(A.pop(0))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import numpy as np

k = int(input())

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

a = 10

while True:
    a_str = str(a)
    a_list = np.array([int(i) for i in a_str])
    a_sa_list = np.diff(a_list)
    if np.abs(a_sa_list).max() == 0 or np.abs(a_sa_list).max() == 1:
        lunlun_list.append(a)
    a += 1
    if len(lunlun_list) >= k:
        print(lunlun_list[k - 1])
        exit()

Submission status: Time Limit Exceeded

Solution code:
K = int(input())
q = [i for i in range(1, 10)]
for _ in range(K):
  x = q.pop(0)
  r = x%10
  if r != 0: q.append(x*10 + r - 1)
  q.append(x*10 + r)
  if r != 9: q.append(x*10 + r + 1)
print(x)

Submission status: Time Limit Exceeded

Solution code:

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

    lunluns = list(range(1, 10))

    lunlun = -1

    for _ in range(k):
        lunlun = lunluns.pop(0)

        ones_place = lunlun % 10
        if ones_place > 0:
            lunluns.append(lunlun * 10 + ones_place -1)
        lunluns.append(lunlun * 10 + ones_place)
        if ones_place < 9:
            lunluns.append(lunlun * 10 + ones_place + 1)
    return lunlun

print(main())

Submission status: Time Limit Exceeded

Solution code:
import math

num = int(input())

box = [1,2,3,4,5,6,7,8,9]
k=0



if num < 10:
    print(box[num-1])

else:
    while True:
        #boxに入っている数字がnumと同じ個数だったら終わり
        if len(box)==num:
            print(box[num-1])
            break

        k = k+1 #k=数字
        k1 = str(k)
        for i in range(len(k1)-1):
            if i == len(k1)-2:
                if abs(int(k1[i])-int(k1[i+1]))<=1:
                    box.append(k)
            if abs(int(k1[i])-int(k1[i+1]))<=1:
                continue
            else:break

Submission status: Time Limit Exceeded

Solution code:
K = int(input())

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

for _ in range(K):
    x = queue.pop(0)
    xmod = x % 10
    if xmod != 0:
        queue.append(10 * x + xmod - 1)
    queue.append(10 * x + xmod)
    if xmod != 9:
        queue.append(10 * x + xmod + 1)
print(x)

Submission status: Time Limit Exceeded

Solution code:
K = int(input())

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

for _ in range(K):
    x = queue.pop(0)
    if x % 10 != 0:
        queue.append(10 * x + x % 10 - 1)
    queue.append(10 * x + x % 10)
    if x % 10 != 9:
        queue.append(10 * x + x % 10 + 1)

print(x)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env pypy3

import sys
import pprint
from collections import deque

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(1)
	n = int(sys.stdin.readline())
	cnt = 0
	stack = deque([1, 2, 3, 4, 5, 6, 7, 8, 9])
	already = deque([])
	while len(stack) != 0:
		now = stack.popleft()
		already.append(now)
		cnt += 1
		if cnt == n:
			print(now)
			exit()
		if now % 10 == 0:
			nexts = [now * 10 + now % 10, now * 10 + now  % 10+ 1]
		elif now % 10 == 9:
			nexts = [now * 10 + now % 10 - 1, now * 10 + now % 10]
		else:
			nexts = [now * 10  + now % 10 - 1, now * 10 + now % 10, now * 10 + now % 10 + 1]
		for next in nexts:
			if next in already or next in stack:
				continue
			stack.append(next)


main()

Submission status: Time Limit Exceeded

Solution code:
K = int(input())

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

c = 0
while c < K: 
    x = queue.pop(0)
    if x % 10 != 0:  
        queue.append(10 * x + x % 10 - 1)
    queue.append(10 * x + x % 10) 
    if x % 10 != 9:
        queue.append(10 * x + x % 10 + 1)
    c += 1

print(x)

Submission status: Time Limit Exceeded

Solution code:
k = int(input())
q = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(k):
    x = q.pop(0)
    y = x % 10
    if y != 0:
        q.append(10 * x + y - 1)
    q.append(10*x + y)
    if y != 9:
        q.append(10 * x + + y +1)
print(x)

Submission status: Time Limit Exceeded

Solution code:
k = int(input())
c = 0
q = [1, 2, 3, 4, 5, 6, 7, 8, 9]
i = 0
while True:
    i = q.pop(0)
    c += 1
    if c == k:
        break
    else:
        e = int(str(i)[-1])
        if e == 0:
            q.append(int(str(i)+str(e)))
            q.append(int(str(i)+str(e+1)))
        elif e == 9:
            q.append(int(str(i)+str(e-1)))
            q.append(int(str(i)+str(e)))
        else:
            q.append(int(str(i)+str(e-1)))
            q.append(int(str(i)+str(e)))
            q.append(int(str(i)+str(e+1)))

print(i)

Submission status: Time Limit Exceeded

Solution code:
x = int(input())

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

ans = 0
index = x

while 1:
    if index <= len(p):
      break
    index -= len(p)
    for i in p:
      for j in range(-1, 2):
          d = i % 10 + j
          if d < 0 or d > 9:
              continue

          nx = i * 10 + d
          p.append(nx)

ans = p[x - 1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
K = int(input())
cnt = 0
i = 1
while cnt < K:
  s = str(i)
  if len(s) == 1:
    cnt += 1
  else:
    flg = True
    for j in range(1, len(s)):
      if abs(int(s[j]) - int(s[j-1])) > 1:
        flg = False
        break
    if flg:
      cnt += 1
  i += 1
print(i-1)

Submission status: Time Limit Exceeded

Solution code:
def f(k):
    def lunlun(n):
        s = [int(i) for i in str(n)]
        for i in range(len(s) - 1):
            if abs(s[i] - s[i + 1]) > 1:
                return False
        return True
    i = 0
    while k > 0:
        i += 1
        if lunlun(i):
            k -= 1
    print(i)

f(int(input()))

Submission status: Time Limit Exceeded

Solution code:
K=int(input())
A=[1,2,3,4,5,6,7,8,9]
I=1
while I<=K:
    B=A.pop(0)
    if not B%10==0:
        A.append(B%10+10*B-1)
    A.append(B%10+10*B)
    if not B%10==9:
        A.append(B%10+10*B+1)
    I+=1
print(B)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())

queue = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
for i in range(n):
    x = queue[i]
    if x % 10 != 0:
        queue = np.append(queue, 10 * x + x % 10 - 1)
    queue = np.append(queue, 10 * x + x % 10)
    if x % 10 != 9:
        queue = np.append(queue, 10 * x + x % 10 + 1)

print(x)

Submission status: Time Limit Exceeded

Solution code:
k = int(input())
que = []
for i in range(k+9):
    if i < 9:
        que.append(i+1)
    else:
        x = que.pop(0)
        if x % 10 != 0:
            que.append(10*x+(x%10) -1)
        que.append(10*x+(x%10))
        if x % 10 != 9:
             que.append(10*x+(x%10) +1)
            
print(x)

Submission status: Time Limit Exceeded

Solution code:
k=int(input())

lis = [i for i in range(1,10)]
for _ in range(k-1):
    x=lis.pop(0)
    if x%10!=0:
        lis.append(10*x+(x%10)-1)
    lis.append(10*x+(x%10))
    if x%10!=9:
        lis.append(10*x+(x%10)+1)
print(lis.pop(0))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

queue = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for _ in range(n):
    x = queue.pop(0)
    if x % 10 != 0:
        queue.append(10 * x + x % 10 - 1)
    queue.append(10 * x + x % 10)
    if x % 10 != 9:
        queue.append(10 * x + x % 10 + 1)

print(x)

Submission status: Time Limit Exceeded

Solution code:
K = int(input())
count = 0
num = 1

while count < K:
    judge = 1
    for i in range(len(str(num))):
        if abs(int(str(num)[i-1]) - int(str(num)[i])) <= 1 and i>= 1:
            judge += 1
    if judge == len(str(num)):
        count += 1
    num += 1
print(num-1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

    counter = 1
    lunlun_counter = 0

    while(True):
        i = str(counter)
        if(len(i) == 1):
            lunlun_counter += 1
        else:
            if(lunlun_judge(i)):
                lunlun_counter += 1
        if(int(lunlun_counter) == K):
            print(int(i))
            break;
        counter = counter+1



def lunlun_judge(str):
    for i in range(len(str)-1):
        if(abs(int(str[i])-int(str[i+1])) > 1):
            return False
    return True

Main()