Skip to content
LC-0390 Medium LeetCode

390. Elimination Game

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 45% Topics: Math, Recursion
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(logn)
# Space: O(1)

class Solution(object):
    def lastRemaining(self, n):
        """
        :type n: int
        :rtype: int
        """
        start, step, direction = 1, 2, 1
        while n > 1:
            start += direction * (step * (n//2) - step//2)
            n //= 2
            step *= 2
            direction *= -1
        return start

Solution from kamyu104/LeetCode-Solutions · MIT

Similar questions