O(LogN) Problem 2 Solution in Python


  • 0
    U
    class Solution(object):
        def lastRemaining(self, n):
            """
            :type n: int
            :rtype: int
            """
            low = 1
            high = n
            last = 1
            current = 2
            flag = True
            while low != high:
                if flag:
                    flag = False
                    if (high - low) % current == 0:
                        high -= current/2
                    low += current/2
                else:
                    flag = True
                    if (high - low) % current == 0:
                        low += current/2
                    high -= current/2
                current *= 2
    
            return low
    

Log in to reply
 

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