Easy Java Solution


  • 0
    L

    Easy Java Solution

    public class Solution {
        public int lastRemaining(int n) {
            return aux(n, true);
        }
        
        private int aux(int n, boolean toRight) {
            if (n == 1)
                return 1;
            if (toRight)
                return 2 * aux(n / 2, false);
            else if (n % 2 == 0)
                return 2 * aux(n / 2, true) - 1;
            else
                return 2 * aux(n / 2, true);
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.