Problem: 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.

Submission attempts (in order):
import sys

K = int(input())

NLUNLUN = [[-1 for _ in range(11)] for _ in range(10)]
def num_lunlun(n, digit):
    if digit == 1:
        NLUNLUN[n][digit] = 1
        return NLUNLUN[n][digit]

    if digit == 2:
        if 0 < n < 9:
            NLUNLUN[n][digit] = 3
        else:
            NLUNLUN[n][digit] = 2
        return NLUNLUN[n][digit]

    n2 = 0
    if n - 1 >= 0:
        n2 += NLUNLUN[n-1][digit-1]
    n2 += NLUNLUN[n][digit-1]
    if n + 1 <= 9:
        n2 += NLUNLUN[n+1][digit-1]
    NLUNLUN[n][digit] = n2
    return NLUNLUN[n][digit]


if K < 10:
    print(K)

k = 0
for digit in range(1, 11):
    for n in range(10):
        tmp = num_lunlun(n, digit)
        if n > 0:
            if k + tmp >= K:
                print(n, digit)
                ans = str(n)
                n2 = n
                for digit2 in range(digit-1, 1, -1):
                    if n2 - 1 >= 0:
                        tmp = NLUNLUN[n2 - 1][digit2]
                        if k + tmp >= K:
                            ans += str(n2 - 1)
                            n2 -= 1
                            continue
                        k += tmp

                    tmp = NLUNLUN[n2][digit2]
                    if k + tmp >= K:
                        ans += str(n2)
                        continue
                    k += tmp

                    if n2 + 1 <= 9:
                        tmp = NLUNLUN[n2 + 1][digit2]
                        if k + tmp >= K:
                            ans += str(n2 + 1)
                            n2 += 1
                            continue
                        k += tmp

                ans = int(ans + str(n2 + K - k - 2))
                print(ans)

                sys.exit(0)
            k += tmp


import sys

K = int(input())

NLUNLUN = [[-1 for _ in range(11)] for _ in range(10)]
def num_lunlun(n, digit):
    if digit == 1:
        NLUNLUN[n][digit] = 1
        return NLUNLUN[n][digit]

    if digit == 2:
        if 0 < n < 9:
            NLUNLUN[n][digit] = 3
        else:
            NLUNLUN[n][digit] = 2
        return NLUNLUN[n][digit]

    n2 = 0
    if n - 1 >= 0:
        n2 += NLUNLUN[n-1][digit-1]
    n2 += NLUNLUN[n][digit-1]
    if n + 1 <= 9:
        n2 += NLUNLUN[n+1][digit-1]
    NLUNLUN[n][digit] = n2
    return NLUNLUN[n][digit]


if K < 10:
    print(K)
    sys.exit(0)

k = 0
for digit in range(1, 11):
    for n in range(10):
        tmp = num_lunlun(n, digit)
        if n > 0:
            if k + tmp >= K:
                ans = str(n)
                n2 = n
                for digit2 in range(digit-1, 1, -1):
                    if n2 - 1 >= 0:
                        tmp = NLUNLUN[n2 - 1][digit2]
                        if k + tmp >= K:
                            ans += str(n2 - 1)
                            n2 -= 1
                            continue
                        k += tmp

                    tmp = NLUNLUN[n2][digit2]
                    if k + tmp >= K:
                        ans += str(n2)
                        continue
                    k += tmp

                    if n2 + 1 <= 9:
                        tmp = NLUNLUN[n2 + 1][digit2]
                        if k + tmp >= K:
                            ans += str(n2 + 1)
                            n2 += 1
                            continue
                        k += tmp

                if ans[-1] == '0':
                    ans = int(ans + str(n2 + K - k - 1))
                else:
                    ans = int(ans + str(n2 + K - k - 2))
                print(ans)

                sys.exit(0)
            k += tmp


Verdicts: Runtime Error

Accepted