Leetcode

Elimination Game

  • Time:O(\log n)
  • Space:O(\log n)

C++

class Solution {
 public:
  int lastRemaining(int n) {
    return n == 1 ? 1 : 2 * (1 + n / 2 - lastRemaining(n / 2));
  }
};

JAVA

class Solution {
  public int lastRemaining(int n) {
    return n == 1 ? 1 : 2 * (1 + n / 2 - lastRemaining(n / 2));
  }
}