-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily21.cpp
More file actions
42 lines (37 loc) · 915 Bytes
/
Copy pathdaily21.cpp
File metadata and controls
42 lines (37 loc) · 915 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
34
35
36
37
38
39
40
41
42
// Solution 1
class Solution {
public:
int findTheWinner(int n, int k) {
auto eliminated = std::unordered_set<int>{};
auto out = 0;
while (eliminated.size() < n - 1) {
auto count = 0;
while (count < k) {
out = (out + 1) % n;
if (out == 0)
out = n;
if (!eliminated.contains(out))
count++;
}
eliminated.insert(out);
}
for (int winner = 1; winner <= n; ++winner) {
if (!eliminated.contains(winner)) {
return winner;
}
}
return -1;
}
};
// Solution 2
class Solution {
public:
int solve(int n, int k){
if (n == 1)
return 0;
return (solve(n - 1, k) + k) % n;
}
int findTheWinner(int n, int k) {
return solve(n, k) + 1;
}
};