Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
585 B
3
Indexable
from collections import deque

def min_moves_to_reduce(N):
    if N == 1:
        return 0
    
    queue = deque([(N, 0)])
    visited = {N}
    
    while queue:
        current, moves = queue.popleft()
        
        for next_state in [current - 1, current // 2, current - 2 * (current // 3)]:
            if next_state == 1:
                return moves + 1
            if next_state not in visited and next_state > 1:
                visited.add(next_state)
                queue.append((next_state, moves + 1))
    
    return -1

N = int(input())
print(min_moves_to_reduce(N))
Leave a Comment