{"id":7191,"date":"2020-08-01T22:03:27","date_gmt":"2020-08-02T05:03:27","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7191"},"modified":"2020-08-01T22:04:22","modified_gmt":"2020-08-02T05:04:22","slug":"leetcode-1535-find-the-winner-of-an-array-game","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1535-find-the-winner-of-an-array-game\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1535. Find the Winner of an Array Game"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>arr<\/code>&nbsp;of&nbsp;<strong>distinct<\/strong>&nbsp;integers and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>A game will be played between the first two elements of the array (i.e.&nbsp;<code>arr[0]<\/code>&nbsp;and&nbsp;<code>arr[1]<\/code>). In each round of the game, we compare&nbsp;<code>arr[0]<\/code>&nbsp;with&nbsp;<code>arr[1]<\/code>, the larger integer&nbsp;wins and remains at position&nbsp;<code>0<\/code>&nbsp;and the smaller integer moves to the end of the array. The game ends when an integer wins&nbsp;<code>k<\/code>&nbsp;consecutive rounds.<\/p>\n\n\n\n<p>Return&nbsp;<em>the integer which will win the game<\/em>.<\/p>\n\n\n\n<p>It is&nbsp;<strong>guaranteed<\/strong>&nbsp;that there will be a winner of the game.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [2,1,3,5,4,6,7], k = 2\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> Let's see the rounds of the game:\nRound |       arr       | winner | win_count\n  1   | [2,1,3,5,4,6,7] | 2      | 1\n  2   | [2,3,5,4,6,7,1] | 3      | 1\n  3   | [3,5,4,6,7,1,2] | 5      | 1\n  4   | [5,4,6,7,1,2,3] | 5      | 2\nSo we can see that 4 rounds will be played and 5 is the winner because it wins 2 consecutive games.\n<\/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> arr = [3,2,1], k = 10\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> 3 will win the first 10 rounds consecutively.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1,9,8,2,3,7,6,4,5], k = 7\n<strong>Output:<\/strong> 9\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1,11,22,33,44,55,66,77,88,99], k = 1000000000\n<strong>Output:<\/strong> 99\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= arr.length &lt;= 10^5<\/code><\/li><li><code>1 &lt;= arr[i] &lt;= 10^6<\/code><\/li><li><code>arr<\/code>&nbsp;contains&nbsp;<strong>distinct<\/strong>&nbsp;integers.<\/li><li><code>1 &lt;= k &lt;= 10^9<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Simulation with a List<\/strong><\/h2>\n\n\n\n<p>Observation : if k &gt;= n &#8211; 1, ans = max(arr) <\/p>\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++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int getWinner(vector<int>& arr, int k) {\n    if (k >= arr.size()) return *max_element(begin(arr), end(arr));\n    int win = 0;    \n    list<int> a(begin(arr), end(arr));\n    while (true) {\n      auto it0 = begin(a);\n      auto it1 = next(it0);\n      if (*it0 > *it1) {\n        a.splice(a.end(), a, it1);\n        ++win;\n      } else {\n        swap(it0, it1);\n        a.splice(a.end(), a, it1);\n        win = 1;\n      }\n      if (win == k) return *it0;\n    }\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: One pass<\/strong><\/h2>\n\n\n\n<p>Since smaller numbers will be put to the end of the array, we must compare the rest of array before meeting those used numbers again. And the winner is monotonically increasing. So we can do it in one pass, just keep the largest number seen so far. If we reach to the end of the array, arr[0] will be max(arr) and it will always win no matter what k is.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int getWinner(vector<int>& arr, int k) {\n    int winner = arr[0];\n    int win = 0;\n    for (int i = 1; i < arr.size(); ++i) {\n      if (arr[i] > winner) {\n        winner = arr[i];\n        win = 0;\n      }\n      if (++win == k) break;\n    }\n    return winner;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"java\">\nclass Solution {\n  public int getWinner(int[] arr, int k) {\n    int winner = arr[0];\n    int win = 0;\n    for (int i = 1; i < arr.length &#038;&#038; win < k; ++i, ++win)\n      if (arr[i] > winner) {\n        winner = arr[i];\n        win = 0;\n      }\n    return winner;\n  }\n}\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def getWinner(self, arr: List[int], k: int) -> int:\n    winner = arr[0]\n    win = 0\n    for x in arr[1:]:\n      if x > winner:\n        winner, win = x, 0\n      win += 1\n      if win == k: break\n    return winner\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;arr&nbsp;of&nbsp;distinct&nbsp;integers and an integer&nbsp;k. A game will be played between the first two elements of the array (i.e.&nbsp;arr[0]&nbsp;and&nbsp;arr[1]). In each round of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,177,179],"class_list":["post-7191","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7191","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=7191"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7191\/revisions"}],"predecessor-version":[{"id":7193,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7191\/revisions\/7193"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}