I'm having some trouble getting the correct solution for the following problem:
Your goal is given a positive integer n, find the minimum number ofoperations needed to obtain the number n starting from the number 1.
More specifically the test case I have in the comments below.
# Failed case #3/16: (Wrong answer)# got: 15 expected: 14# Input:# 96234## Your output:# 15# 1 2 4 5 10 11 22 66 198 594 1782 5346 16038 16039 32078 96234# Correct output:# 14# 1 3 9 10 11 22 66 198 594 1782 5346 16038 16039 32078 96234# (Time used: 0.10/5.50, memory used: 8601600/134217728.)def optimal_sequence(n):sequence = []while n >= 1:sequence.append(n)if n % 3 == 0:n = n // 3optimal_sequence(n)elif n % 2 == 0:n = n // 2optimal_sequence(n)else:n = n - 1optimal_sequence(n)return reversed(sequence)input = sys.stdin.read()n = int(input)sequence = list(optimal_sequence(n))print(len(sequence) - 1)for x in sequence:print(x, end=' ')
It looks like I should be outputting 9 where I'm outputting 4 & 5 but I'm not sure why this isn't the case. What's the best way to troubleshoot this problem?