-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
390.java
31 lines (30 loc) · 979 Bytes
/
390.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
__________________________________________________________________________________________________
sample 2 ms submission
class Solution {
public int lastRemaining(int n) {
int start = 1, end = n;
int jump = 1;
boolean flip = true;
while (start != end) {
if (flip) {
start += jump;
jump *= 2;
end -= (end-start)%jump;
} else {
end -= jump;
jump *= 2;
start += (end-start)%jump;
}
flip = !flip;
}
return start;
}
}
__________________________________________________________________________________________________
sample 33568 kb submission
class Solution {
public int lastRemaining(int n) {
return n == 1 ? 1 : 2 * (1 + n / 2 - lastRemaining(n / 2));
}
}
__________________________________________________________________________________________________