1
class Solution {
2
public:
3
vector<int> earliestAndLatest(int n, int first, int second) {
4
int minRound = INT_MAX, maxRound = INT_MIN;
5

6
function<void(int, int, int, int)> dfs = [&](int deadMask, int i, int j, int curRound) {
7
while (i < j and deadMask & (1 << i)) // 'i' is dead warrior, try next
8
i += 1;
9

10
while (i < j and deadMask & (1 << j)) // 'j' is dead warrior, try next
11
j -= 1;
12

13
if (i >= j) // end of round, no more fights possible
14
dfs(deadMask, 1, n, curRound + 1);
15

16
else if (i == first and j == second) // BATTLE OF THE IMMORTALS
17
minRound = min(curRound, minRound), maxRound = max(curRound, maxRound);
18

19
else { // BATTLE includes a mortal
20
if (i != first and i != second) // 'i' is MORTAL, he may die
21
dfs(deadMask | (1 << i), i + 1, j - 1, curRound);
22
if (j != first and j != second) // 'j' is MORTAL, he may die
23
dfs(deadMask | (1 << j), i + 1, j - 1, curRound);
24
}
25
};
26
dfs(0, 1, n, 1);
27
return {minRound, maxRound};
28
}
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0