问题:
There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.
Repeat the previous step again, but this time from right to left, remove the right most number and every other number from the remaining numbers.
We keep repeating the steps again, alternating left to right and right to left, until a single number remains.
Find the last number that remains starting with a list of length n.
Example:
Input: n = 9, 1 2 3 4 5 6 7 8 9 2 4 6 8 2 6 6 Output: 6
解决:
【分析】
首先设输入数据为 n 时,最终结果为 f(n).很显然,有 f(1)=1,f(2)=2.
当 n=2k 时,从1,3,5,... ,2k-1 删除奇数项后,剩余的是2,4,6,... ,2k, 共 k 个偶数项。
当 n=2k+1 时,从1,3,5,... ,2k-1 ,2k+1 删除奇数项后,剩余的是2,4,6,... ,2k, 共 k 个偶数项。这两种情况在从左到右删除一遍后,剩下的内容相同,所以最终剩下的一项也相同,所以我们可以得出结论:
f(2k+1)=f(2k) 所以,我们只需考虑 n 为偶数的情况。
① 我们用一个boolean型变量left2right,为true表示从左往右,为false表示从右往左遍历。
当n为1时,不论从左往右还是从右往左都返回1。
如果n大于1,且是从左往右的话,我们返回2倍的对n/2的从右往左的遍历;
如果是从右往左的话,稍稍麻烦一些,我们肯定还是要对n/2调用递归函数的,但是要分奇偶情况,如果n为奇数,返回2倍的对n/2的从左往右的遍历的值;如果n为偶数,2倍的对n/2的从左往右的遍历的值,再减去1。
递归方法解决:
class Solution { //80ms
public int lastRemaining(int n) {
return dfs(n,true);
}
public int dfs(int n,boolean left2right){
if (n == 1) return 1;
if (left2right){
return 2 * dfs(n / 2,false);
}else {
return 2 * dfs(n / 2,true) - 1 + n % 2;
}
}
}
② 第一次从左往右删除的时候,奇数都被删掉了,剩下的都是偶数。如果我们对所有数都除以2,那么得到一个1到n/2的新数列。下一次我们从右往左删出,那么返回的结果应该是调用递归的结果lastRemaining(n / 2)在数组1到n/2之间的镜像。何为镜像,比如1, 2, 3, 4这个数字,2的镜像就是3, 1的镜像是4。
class Solution { //81ms
public int lastRemaining(int n) {
return n == 1 ? 1 : 2 * ( 1 + n / 2 - lastRemaining(n / 2));
}
}