Python beat 100%


  • 0

    Simply keep the start, end and the gap between deletions for every round. The code is straightforward.

    class Solution(object):
        def lastRemaining(self, n):
            """
            :type n: int
            :rtype: int
            """
            count = n
            start = 1
            end = n
            direction = 1
            gap = 2
            while start < end:
                if direction:
                    if (end-start) % gap == 0:
                        end -= gap / 2
                    start = start + gap / 2
                    direction = 0
                else:
                    if (end - start) % gap == 0:
                        start = start + gap / 2
                    end = end - gap/2
                    direction = 1
                gap *= 2
            return start
    

Log in to reply
 

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