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:
K = input()
K = int(K)


def f(K):
    cnt =0
    num= 1
    while(1):
        listmp  =  [int(x) for x in list(str(num))]

        if num<10:
            cnt +=1
            num+=1
        else:
            flag  =0
            for i in range(len(listmp)-1):
                if abs(listmp[i]-listmp[i+1]) > 1:
                    flag = 1
                # print('diff',listmp[i]-listmp[i+1])
            if flag == 0:
                cnt +=1


            # print('listmp',listmp,'cnt',cnt)
            if K == cnt:
                return num
            num +=1


print(f(K))

Submission status: Time Limit Exceeded

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

    lunlun_number_memo = {}

    # def is_lunlun(str_n):
    #     while True:
    #         if lunlun_number_memo.get(str_n):
    #             return True
    #         elif len(str_n) == 1:
    #             return True
    #         else:
    #             if abs(int(str_n[0]) - int(str_n[1])) <= 1:
    #                 str_n = str_n[1:]
    #             else:
    #                 return False


    lunlun_number_count = 0
    i = 0
    while lunlun_number_count < K:
        i += 1
        # if is_lunlun(str(i)):
        str_i = str(i)
        while True:
            if lunlun_number_memo.get(str_i) or len(str_i) == 1:
                lunlun_number_memo[str(i)] = True
                lunlun_number_count += 1
                break
            else:
                if abs(int(str_i[0]) - int(str_i[1])) <= 1:
                    str_i = str_i[1:]
                else:
                    break

            # print(i)
            # print(lunlun_number_count)
            # print("running")

    print(i)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
count = 0
def check(x):
    f= True
    if(x < 10):
        return True
    x = str(x)
    for i in range(len(x)-1):
        if( int(x[i]) - int(x[i + 1]) != 0):
            f = False
        if( int(x[i]) - int(x[i + 1]) != 1 ):
            f = False
        if( int(x[i]) - int(x[i + 1]) != -1 ):
            f = False
        if(f == False):
            return False
    return True
i = 0    
while(True):
    i = i + 1
    if(check(i)):
    #    print(i)
        count = count + 1 
        if(count == num ):
            print(i)
            exit()

Submission status: Time Limit Exceeded

Solution code:
K = int(input())
 
 
h = 10
cnt = 9
while h <= 10 ** 100:
    X = str(h)
    N = len(X)
 
    i = 0
    ans = 'Yes'
    while i < N-1:
        if (int(X[i]) - int(X[i+1]) == 1) or (int(X[i+1]) - int(X[i]) == 1) or (int(X[i]) == int(X[i+1])):
            i += 1
        else:
            ans = 'No'
            break
    if ans == 'Yes':
        cnt += 1
    
    if cnt == K:
        print(h)
        break
    else:
        h += 1

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
#pypyではscipy, numpyは使えない
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
#from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
import numpy as np
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7

def main():
  k= int(readline())
  if k<10:
    print(k)
    sys.exit()
  dp = np.ones((10,10), dtype='int64')
  #dp[i-1][j]は、i桁のときの末尾がjの数
  dp[0, 0] = 0
  for i in range(9):
    for j in range(10):
      if j == 0:
        dp[i+1,0] = dp[i,0] + dp[i,1]
      elif j == 9:
        dp[i+1,j] = dp[i,8] + dp[i,9]
      else:
        dp[i+1,j] = dp[i,j-1] + dp[i,j] + dp[i,j+1]
  total = [0]
  #i桁のときの数の累積和
  for i in range(10):
    tmp = total[-1] + dp[i,:].sum()
    total.append(tmp)
  keta = 0
  for i in range(11):
    if total[i]<=k:
      keta = i+1
  #kはketa桁である
  c = 10**(keta-1)
  count = total[keta-1]
  for i in range(c,c*10):
    s = str(i)
    bl = True
    for j in range(keta-1):
      if abs(int(s[j])-int(s[j+1]))>1:
        bl=False
    if bl:
      count +=1
    if count == k:
      print(i)
      sys.exit()
  
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
import copy
k = int(input())

cnt = 0
for i in range(1, 100000):
  if i < 9:
    cnt += 1
  else:
    num = copy.copy(i)
    nei = []
    while num < 10:
      nei.append(num%10)
      num /= 10
    for j in range(len(nei)):
      if abs(nei[j] - nei[j+1]) > 1:
        break
      elif abs(nei[j] - nei[j+1]) <= 1 and nei[j] == nei[len(nei)-1]:
        cnt += 1
        
  if cnt == k:
    print(i)
    break

Submission status: Time Limit Exceeded

Solution code:
k = int(input())
count = 0
for j in range(3234566668):
    judge = 1
    if 1 <= j + 1 <= 9:
        count += 1
    else:
        s = str(j + 1)
        for i in range(len(s) - 1):
            if abs(int(s[i]) - int(s[i + 1])) > 1:
                judge = 0
                break
        count += judge
    if count == k:
        break
print(j + 1)

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
run=[0]*a
cnt=1
cn=0
while 1:
    while 1:
        w=0
        b=cnt%10
        c=(cnt%100)//10
        if(((b-c)==-1)or((b-c==1))or((b-c==0))):
            pass
        else:
            w=1
        if cnt==0:
            break
    if w==0:
        run[cn]=cnt
        cn=cn+1
        cnt=cnt//10
    cnt=cnt+1
    if cn==a:
        break
print(max(run))