easy O(logN) python solution


  • 0
    L
    class Solution(object):
        def lastRemaining(self, n):
            """
            each round the remaining list can be represented as (start,count,step)
            :type n: int
            :rtype: int
            """
            start, count, step = 1, n, 1
            while count > 1:
                end = start + (count - 1) * step
                # compute the next round
                start = end - (count % 2) * step
                count /= 2
                step *= -2
            return start
    

Log in to reply
 

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