2293. Min Max Game

problem

solution

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 minMaxGame(vector<int>& nums) {
queue<int> q;
for(int a:nums) q.push(a);
while(q.size()!=1){
int n = q.size();
for(int i= 0 ;i<n/2;i++){
if(i%2==0){
int a = q.front();
q.pop();
if(!q.empty()){
int b = q.front();
q.pop();
q.push(min(a, b));
}
}
else{
int a = q.front();
q.pop();
if(!q.empty()){
int b = q.front();
q.pop();
q.push(max(a, b));
}
}

}
}
return q.front();

}
};

analysis

  • time complexity O()
  • space complexity O()