Skip to content

390. Elimination Game

  • Time: $O(\log n)$
  • Space: $O(\log n)$
1
2
3
4
5
6
class Solution {
 public:
  int lastRemaining(int n) {
    return n == 1 ? 1 : 2 * (1 + n / 2 - lastRemaining(n / 2));
  }
};
1
2
3
4
5
class Solution {
  public int lastRemaining(int n) {
    return n == 1 ? 1 : 2 * (1 + n / 2 - lastRemaining(n / 2));
  }
}
Back to top