-
Notifications
You must be signed in to change notification settings - Fork 6
/
1823.cpp
47 lines (41 loc) · 912 Bytes
/
1823.cpp
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
//Solution 1 :-
class Solution {
public:
int findTheWinner(int n, int k) {
if (n == 1)
return 1;
std::deque<int> deq;
for (int i{1}; i <= n; i++)
deq.push_back(i);
while (!(int(deq.size()) == 2)) {
int curr{k};
while (curr-- > 1) {
int front{deq.front()};
deq.pop_front();
deq.push_back(front);
}
deq.pop_front();
}
return deq[k % 2];
}
};
//Solution 2 :-
class Solution {
public:
int findTheWinner(int n, int k) {
queue<int> q;
for(int i = 1; i <= n; i++)
q.push(i);
while(q.size() != 1){
int temp = k;
while(temp > 1){
int frnt = q.front();
q.pop();
q.push(frnt);
temp--;
}
q.pop();
}
return q.front();
}
};