Leetcode 390 Solution

This article provides solution to leetcode question 390 (elimination-game)

https://leetcode.com/problems/elimination-game

Solution

class Solution:
    def lastRemaining(self, n: int) -> int:
        def last(n, left_to_right):
            if n == 1:
                return 1

            if left_to_right:
                return 2 * last(n // 2, False)
            else:
                return 2 * last(n // 2, True) - (n + 1) % 2

        return last(n, True)