백준 16943.숫자 재배치

이상·2024년 5월 13일

알고리즘

목록 보기
9/21


S1 16943.숫자 재배치

백트래킹 함수를 만드려다 오랜만에 permutations를 사용했다.
초반에 틀렸던 이유가 i[0]이 '0'이면 break를 해서 for문이 멈춰버렸다..ㅋㅋ
그래서 그냥 조건을 0이 아닌 경우로 설정하고 b와 비교한 뒤 ans에 들어가게 해주었다.

from itertools import permutations
a,b=input().split()
a_lst=list(a)
a_lst.sort()
ans=-1
for i in list(permutations(a_lst)):
    if i[0] != '0' and int(b) > int(''.join(i)):
        ans = max(ans,int(''.join(i)))
print(ans)
profile
입니다.

0개의 댓글