Problem statement

https://binarysearch.com/problems/Integer-to-Base-3/

Solution

Variation of Leetcode 504. Base 7.

Complexity

It is O(log n) for time and space.

Code

class Solution:
    def solve(self, num):
        ans, num = "", abs(num)
        while num:
            num, digit = divmod(num, 3)
            ans += str(digit)
            
        return ans[::-1] if ans else "0"