728x90

다 풀었는데 실패가 떴고, 몇몇 반례들이 통과가 안됐으나 이유를 못찼았다. 답을 보니 처음부터 탐색을 해도 풀렸다. 시간 초과 때문에 가운데 부터 탐색을 했는데 안그랬으면 통과했을 것 같다.

import math
 
def find(n,possible):
 
    for i in range(0,len(n)):
        if n[i] not in possible:
            return False
 
 
    return True    
 
 
 
 
a = int(input()) # 찾을 숫자
 
b = int(input())
 
 
if b == 0:
    print(len(list(str(a))))
 
else:
 
    k = list(map(int,input().split(" ")))
 
 
    # 최대한 근접한 수를 누르고 나머지는 차이를 + 해주기
 
    answer1 = abs(100-a)
 
    answer2 = 0
 
    possible = [ ] # 클릭 가능한 버튼 리스트
 
    for i in range(0,10):
        if i not in k:
            possible.append(str(i))
 
    index = 0
 
    if len(possible)==0:
        print(answer1)
    else:
        while True:
 
            first = a + index
            second = a - index
 
            first = list(str(first))
            second = list(str(second))
 
 
            if find(first,possible)==True or find(second,possible)==True:
                break
 
            index += 1
 
            answer2+=1
 
 
        answer2 += len(list(str(a)))
 
        print(min(answer1,answer2))
 
 
 
728x90

+ Recent posts