[
simulate
]
BinarySearch 0133 Collatz Sequence
Problem statement
https://binarysearch.com/problems/Collatz-Sequence/
Solution
We just need to simulate the process.
Complexity
Hopefully it is not big, O(m)
, where m
is the length of sequence, but how long it is, is a good question.
Code
class Solution:
def solve(self, n):
ans = 0
while n != 1:
if n % 2 == 0:
n = n//2
else:
n = 3*n + 1
ans += 1
return ans + 1