-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path390.cpp
More file actions
executable file
·57 lines (57 loc) · 1.85 KB
/
390.cpp
File metadata and controls
executable file
·57 lines (57 loc) · 1.85 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution {
public:
int lastRemaining(int n) {
int size = n;
vector<bool> choose(size,false);
int start = 0;
int emli = 0;
bool left_to_right = true;
while (emli < size - 1){
cout << start << endl;
if (left_to_right){
choose[start] = true;
while (start < size){
while (start < size && choose[start] == true)
start ++;
start ++;
while (start < size && choose[start] == true)
start ++;
if (start < size){
choose[start] = false;
emli ++;
}
if (emli == size - 1)
break;
}
start = size - 1;
//find a number that has never been choosen
while (choose[start] == true)
start --;
}else{
choose[start] = true;
while (start >= 0){
// two false
while (start >= 0 && choose[start] == true)
start --;
start --;
while (start >= 0 && choose[start] == true)
start --;
if (start >= 0){
choose[start] = true;
emli ++;
}
if (emli == size - 1)
break;
}
start = 0;
while (choose[start] == true)
start ++;
}
left_to_right = !left_to_right;
}
for (int i = 0; i < size; i++)
if (choose[i] == false)
return i + 1;
return -1;
}
};