Python Simple Simulation Beats 100%


  • 0

    The first element in each iteration is either the first one or the second one from the previous sequence and in each iteration, the difference between first and second element increases exponentially. So, record the value of the first element in each iteration and increase the step to get the next first element.

    class Solution(object):
        def lastRemaining(self, n):
            """
            :type n: int
            :rtype: int
            """
            count = n
            head = 1
            isFromLeft = True
            step = 1
            while count > 1:
                if isFromLeft or count % 2 == 1:
                    head = head + step
                count /= 2
                step *= 2
                isFromLeft = not isFromLeft
            return head
    

Log in to reply
 

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