|
| 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