1823. Find the Winner of the Circular Game

There are n friends that are playing a game. The friends are sitting in a circle and are numbered from 1 to n in clockwise order. More formally, moving clockwise from the ith friend brings you to the (i+1)th friend for 1 <= i < n, and moving clockwise from the nth friend brings you to the 1st friend.

The rules of the game are as follows:

  1. Start at the 1st friend.
  2. Count the next k friends in the clockwise direction including the friend you started at. The counting wraps around the circle and may count some friends more than once.
  3. The last friend you counted leaves the circle and loses the game.
  4. If there is still more than one friend in the circle, go back to step 2 starting from the friend immediately clockwise of the friend who just lost and repeat.
  5. Else, the last friend in the circle wins the game.

Given the number of friends, n, and an integer k, return the winner of the game.

class Solution {
public:
     int findTheWinner(int n, int k) {
        queue<int> qu;
        for (int i = 1; i <= n; i++) {
            qu.emplace(i);
        }
        while (qu.size() > 1) {
            for (int i = 1; i < k; i++) {
                qu.emplace(qu.front());
                qu.pop();
            }
            qu.pop();
        }
        return qu.front();
    }
};

相关推荐

  1. DAY123

    2024-03-24 22:00:03       31 阅读
  2. 1820D-The Butcher

    2024-03-24 22:00:03       30 阅读
  3. H12-821_182

    2024-03-24 22:00:03       38 阅读
  4. matlab NO123

    2024-03-24 22:00:03       35 阅读
  5. 题解:CF1923D(Slimes)

    2024-03-24 22:00:03       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-24 22:00:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-24 22:00:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-24 22:00:03       82 阅读
  4. Python语言-面向对象

    2024-03-24 22:00:03       91 阅读

热门阅读

  1. 【python】(09)理解Python中的zip()和zip(*iterable)

    2024-03-24 22:00:03       42 阅读
  2. 力扣刷题之20.有效的括号

    2024-03-24 22:00:03       41 阅读
  3. 2024.03.08 校招 实习 内推 面经

    2024-03-24 22:00:03       39 阅读
  4. ubuntu20.04 安装ros1

    2024-03-24 22:00:03       42 阅读
  5. Linux权限理解

    2024-03-24 22:00:03       39 阅读
  6. python基础24(完结撒花)_习题总结

    2024-03-24 22:00:03       46 阅读
  7. pta L1-076 降价提醒机器人

    2024-03-24 22:00:03       35 阅读
  8. Rancher(v2.6.3)——Rancher部署Mysql(单机版)

    2024-03-24 22:00:03       39 阅读
  9. dddssss

    2024-03-24 22:00:03       36 阅读
  10. leetcode-Two Sum

    2024-03-24 22:00:03       32 阅读