-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1900_leetcode
More file actions
34 lines (33 loc) · 969 Bytes
/
Copy path1900_leetcode
File metadata and controls
34 lines (33 loc) · 969 Bytes
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
class Solution {
public:
int mx = INT_MIN, mn = INT_MAX;
int N = 0, f = 0, s = 0;
void calc(int mask,int round,int i,int j){
if(i>=j){
calc(mask,round+1,0,N); // increment round and reset pointers
}
else if((mask&(1<<i)) == 0){
calc(mask,round,i+1,j);
}
else if((mask&(1<<j)) == 0){
calc(mask,round,i,j-1);
}
else if(i==f && j==s){ // best players comes head to head
mx = max(mx,round);
mn = min(mn,round);
}
else{
if(i!=f && i!=s){
calc(mask^(1<<i),round,i+1,j-1);
}
if(j!=f && j!=s){
calc(mask^(1<<j),round,i+1,j-1);
}
}
}
vector<int> earliestAndLatest(int n, int firstPlayer, int secondPlayer) {
N = n-1; f = firstPlayer - 1; s = secondPlayer -1;
calc((1<<n)-1,1,0,n-1);
return {mn,mx};
}
};