{"id":8342,"date":"2021-04-12T23:28:01","date_gmt":"2021-04-13T06:28:01","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8342"},"modified":"2021-04-12T23:28:51","modified_gmt":"2021-04-13T06:28:51","slug":"leetcode-1823-find-the-winner-of-the-circular-game","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1823-find-the-winner-of-the-circular-game\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1823. Find the Winner of the Circular Game"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;friends that are playing a game. The friends are sitting in a circle and are numbered from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>&nbsp;in&nbsp;<strong>clockwise order<\/strong>. More formally, moving clockwise from the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;friend brings you to the&nbsp;<code>(i+1)<sup>th<\/sup><\/code>&nbsp;friend for&nbsp;<code>1 &lt;= i &lt; n<\/code>, and moving clockwise from the&nbsp;<code>n<sup>th<\/sup><\/code>&nbsp;friend brings you to the&nbsp;<code>1<sup>st<\/sup><\/code>&nbsp;friend.<\/p>\n\n\n\n<p>The rules of the game are as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Start<\/strong>&nbsp;at the&nbsp;<code>1<sup>st<\/sup><\/code>&nbsp;friend.<\/li><li>Count the next&nbsp;<code>k<\/code>&nbsp;friends in the clockwise direction&nbsp;<strong>including<\/strong>&nbsp;the friend you started at. The counting wraps around the circle and may count some friends more than once.<\/li><li>The last friend you counted leaves the circle and loses the game.<\/li><li>If there is still more than one friend in the circle, go back to step&nbsp;<code>2<\/code>&nbsp;<strong>starting<\/strong>&nbsp;from the friend&nbsp;<strong>immediately clockwise<\/strong>&nbsp;of the friend who just lost and repeat.<\/li><li>Else, the last friend in the circle wins the game.<\/li><\/ol>\n\n\n\n<p>Given the number of friends,&nbsp;<code>n<\/code>, and an integer&nbsp;<code>k<\/code>, return&nbsp;<em>the winner of the game<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/03\/25\/ic234-q2-ex11.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5, k = 2\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> Here are the steps of the game:\n1) Start at friend 1.\n2) Count 2 friends clockwise, which are friends 1 and 2.\n3) Friend 2 leaves the circle. Next start is friend 3.\n4) Count 2 friends clockwise, which are friends 3 and 4.\n5) Friend 4 leaves the circle. Next start is friend 5.\n6) Count 2 friends clockwise, which are friends 5 and 1.\n7) Friend 1 leaves the circle. Next start is friend 3.\n8) Count 2 friends clockwise, which are friends 3 and 5.\n9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 6, k = 5\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= k &lt;= n &lt;= 500<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Simulation w\/ Queue<\/strong> <strong>\/ List<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n*k)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++\/Queue<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, 56 ms, 24.3 MB\nclass Solution {\npublic:\n  int findTheWinner(int n, int k) {\n    queue<int> q;\n    for (int i = 1; i <= n; ++i)\n      q.push(i);\n    \n    while (q.size() != 1) {\n      for (int i = 1; i < k; ++i) {\n        int x = q.front();\n        q.pop();\n        q.push(x);\n      }\n      q.pop();        \n    }\n    return q.front();\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++\/List<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, 12 ms, 6.8 MB\nclass Solution {\npublic:\n  int findTheWinner(int n, int k) {\n    list<int> l;\n    for (int i = 1; i <= n; ++i)\n      l.push_back(i);\n    \n    auto it = l.begin();\n    while (l.size() != 1) {\n      for (int i = 1; i < k; ++i)\n        if (++it == l.end()) \n          it = l.begin();\n      it = l.erase(it);\n      if (it == l.end()) \n        it = l.begin();\n    }\n    return l.front();\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;friends that are playing a game. The friends are sitting in a circle and are numbered from&nbsp;1&nbsp;to&nbsp;n&nbsp;in&nbsp;clockwise order. More formally, moving clockwise from the&nbsp;ith&nbsp;friend&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[83,31,213,179],"class_list":["post-8342","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-list","tag-math","tag-queue","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8342"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8342\/revisions"}],"predecessor-version":[{"id":8345,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8342\/revisions\/8345"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}