Skip to content

Commit 613ef3c

Browse files
committed
New Problem Solution - "1823. Find the Winner of the Circular Game"
1 parent d26ba3a commit 613ef3c

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LeetCode
99

1010
| # | Title | Solution | Difficulty |
1111
|---| ----- | -------- | ---------- |
12+
|1823|[Find the Winner of the Circular Game](https://leetcode.com/problems/find-the-winner-of-the-circular-game/) | [C++](./algorithms/cpp/findTheWinnerOfTheCircularGame/FindTheWinnerOfTheCircularGame.cpp)|Medium|
1213
|1822|[Sign of the Product of an Array](https://leetcode.com/problems/sign-of-the-product-of-an-array/) | [C++](./algorithms/cpp/signOfTheProductOfAnArray/SignOfTheProductOfAnArray.cpp)|Easy|
1314
|1819|[Number of Different Subsequences GCDs](https://leetcode.com/problems/number-of-different-subsequences-gcds/) | [C++](./algorithms/cpp/numberOfDifferentSubsequencesGcds/NumberOfDifferentSubsequencesGcds.cpp)|Hard|
1415
|1818|[Minimum Absolute Sum Difference](https://leetcode.com/problems/minimum-absolute-sum-difference/) | [C++](./algorithms/cpp/minimumAbsoluteSumDifference/MinimumAbsoluteSumDifference.cpp)|Medium|
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Source : https://leetcode.com/problems/find-the-winner-of-the-circular-game/
2+
// Author : Hao Chen
3+
// Date : 2021-04-11
4+
5+
/*****************************************************************************************************
6+
*
7+
* There are n friends that are playing a game. The friends are sitting in a circle and are numbered
8+
* from 1 to n in clockwise order. More formally, moving clockwise from the i^th friend brings you to
9+
* the (i+1)^th friend for 1 <= i < n, and moving clockwise from the n^th friend brings you to the
10+
* 1^st friend.
11+
*
12+
* The rules of the game are as follows:
13+
*
14+
* Start at the 1^st friend.
15+
* Count the next k friends in the clockwise direction including the friend you started at.
16+
* The counting wraps around the circle and may count some friends more than once.
17+
* The last friend you counted leaves the circle and loses the game.
18+
* If there is still more than one friend in the circle, go back to step 2 starting from the
19+
* friend immediately clockwise of the friend who just lost and repeat.
20+
* Else, the last friend in the circle wins the game.
21+
*
22+
* Given the number of friends, n, and an integer k, return the winner of the game.
23+
*
24+
* Example 1:
25+
*
26+
* Input: n = 5, k = 2
27+
* Output: 3
28+
* Explanation: Here are the steps of the game:
29+
* 1) Start at friend 1.
30+
* 2) Count 2 friends clockwise, which are friends 1 and 2.
31+
* 3) Friend 2 leaves the circle. Next start is friend 3.
32+
* 4) Count 2 friends clockwise, which are friends 3 and 4.
33+
* 5) Friend 4 leaves the circle. Next start is friend 5.
34+
* 6) Count 2 friends clockwise, which are friends 5 and 1.
35+
* 7) Friend 1 leaves the circle. Next start is friend 3.
36+
* 8) Count 2 friends clockwise, which are friends 3 and 5.
37+
* 9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.
38+
*
39+
* Example 2:
40+
*
41+
* Input: n = 6, k = 5
42+
* Output: 1
43+
* Explanation: The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
44+
*
45+
* Constraints:
46+
*
47+
* 1 <= k <= n <= 500
48+
******************************************************************************************************/
49+
50+
class Solution {
51+
public:
52+
int findTheWinner(int n, int k) {
53+
vector<int> persons(n);
54+
for(int i=0; i<n; i++) persons[i] = i + 1;
55+
56+
for(int start=0; n>1; n--){
57+
start = (start + k -1) % n;
58+
persons.erase(persons.begin() + start);
59+
start = start %(n-1);
60+
}
61+
return persons[0];
62+
}
63+
};

0 commit comments

Comments
 (0)