Elimination Game

Problem Id: 390 Difficulty: Medium


Intuition

Solution


class Solution:
    def _right(self, n):
        if n == 1:
            return 1
        if n % 2 == 1:
            return 2 * self._left(n // 2)
        else:
            return 2 * self._left(n // 2) - 1

    def _left(self, n):
        if n == 1:
            return 1
        return 2 * self._right(n // 2)

    def lastRemaining(self, n: int) -> int:
        return self._left(n)