• + 0 comments

    A solution in Python, with some comments.

    def utopianTree(n):
        # Set the initial height of the tree
        height = 1
        # Let's setup a range that'll cover every cycle and iterate through it.
        for cycle in range(0, n + 1):
            # Nothing happens during the first cycle, so we skip the rest of the loop.
            if cycle == 0: continue
            # Odd cycles are 'spring', so we add height to itself, otherwise add 1.
            height += height if cycle % 2 == 1 else 1
        return height