390. Elimination Game¶ Time: $O(\log n)$ Space: $O(\log n)$ C++JavaPython 1 2 3 4 5 6class Solution { public: int lastRemaining(int n) { return n == 1 ? 1 : 2 * (1 + n / 2 - lastRemaining(n / 2)); } }; 1 2 3 4 5class Solution { public int lastRemaining(int n) { return n == 1 ? 1 : 2 * (1 + n / 2 - lastRemaining(n / 2)); } } 1 2 3class Solution: def lastRemaining(self, n: int) -> int: return 1 if n == 1 else 2 * (1 + n // 2 - self.lastRemaining(n // 2))