프로그래머스 - 숫자변환하기 (파이썬)
이 문제는 범위에 제한이 있어야 풀리는 문제이다. dfs나 bfs를 사용하면 depth, width가 무한정 커질 수 있기 때문에 배열을 사용하여 범위에 제한을 두고 for문을 돌려야 한다. 1. dfs 사용(x) answer=1000000 def dfs(x,n,y,tot,cnt): global answer if tot>=x: if tot==x: answer=min(answer,cnt) return if tot%3==0: dfs(x,n,y,tot//3,cnt+1) if tot%2==0: dfs(x,n,y,tot//2,cnt+1) dfs(x,n,y,tot-n,cnt+1) def solution(x, y, n): global answer dfs(x,n,y,y,0) if answer==1000000: retu..
2024. 4. 14.