미래의 나를 위한 메모/코딩테스트

[230113] 백준 1107번 - 리모컨 (Python)

Choi Jaekuk 2023. 1. 13. 12:58

https://github.com/cjk09083/Code

https://www.acmicpc.net/problem/1107

 

1107번: 리모컨

첫째 줄에 수빈이가 이동하려고 하는 채널 N (0 ≤ N ≤ 500,000)이 주어진다.  둘째 줄에는 고장난 버튼의 개수 M (0 ≤ M ≤ 10)이 주어진다. 고장난 버튼이 있는 경우에는 셋째 줄에는 고장난 버튼

www.acmicpc.net

 

해답) 

import sys
input = sys.stdin.readline


def isBtnOnly():
    btnOnly = True
    for i in str(t):
        if i in err:
            btnOnly = False
            break

    return btnOnly

def getNum():
    if isBtnOnly():
        return min(abs(t-100),len(str(t)))
    
    start = 0
    end = 1_000_000
    closestNum = abs(t - 100)

    for i in range(start, end):
        tmp = str(i)
        l = len(tmp)
        isBreak = False

        for j in range(l):
            if tmp[j] in err:
                isBreak = True
                break

        if not isBreak:
            c = abs(t - i) + l
            closestNum = min(c, closestNum)
            
    return closestNum

if __name__ == "__main__": 
    t = int(input()) 
    n = int(input())
    if t == 100:
        cnt = 0
    elif n == 0:
        cnt = min(abs(t-100),len(str(t)))
    elif n == 10:
        cnt = abs(t-100)
    else:
        err = list(map(str, input().split()))
        cnt = getNum()

    print(cnt)
- closestNum를 0부터 999999까지 올려가면서 가장 가까운 숫자를 찾는다.
- 708ms