Clean Easy Java logN solution


  • 2
    public class Solution {
        public int lastRemaining(int n) {
            return help(n, true);
        }
        
        private int help(int n, boolean left) {
            if (n == 1) return 1;
            int res = help(n / 2, !left) * 2;
            if ((n & 1) == 1 || left) {
                return res;
            }
            return res - 1;
        }
    }
    

Log in to reply
 

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