Problem 2 Python recursive solution (6 lines)


  • 0
    G
    def lastRemaining(self, n):
        """
        :type n: int
        :rtype: int
        """
    
        def rf(l, reversed):
            if l == 1: return 0
            if reversed and l % 2 == 0:
                return 2 * rf(l//2, not reversed)
            return 2 * rf(l//2, not reversed) + 1
    
        return rf(n, False) + 1

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.