{"id":10036,"date":"2023-04-30T09:41:18","date_gmt":"2023-04-30T16:41:18","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10036"},"modified":"2023-04-30T09:42:27","modified_gmt":"2023-04-30T16:42:27","slug":"leetcode-2660-determine-the-winner-of-a-bowling-game","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-2660-determine-the-winner-of-a-bowling-game\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2660. Determine the Winner of a Bowling Game"},"content":{"rendered":"\n<p>You are given two&nbsp;<strong>0-indexed<\/strong>&nbsp;integer arrays&nbsp;<code>player1<\/code>&nbsp;and&nbsp;<code>player2<\/code>, that represent the number of pins that player 1 and player 2 hit in a bowling game, respectively.<\/p>\n\n\n\n<p>The bowling game consists of&nbsp;<code>n<\/code>&nbsp;turns, and the number of pins in each turn is exactly&nbsp;<code>10<\/code>.<\/p>\n\n\n\n<p>Assume a player hit&nbsp;<code>x<sub>i<\/sub><\/code>&nbsp;pins in the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;turn. The value of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;turn for the player is:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2x<sub>i<\/sub><\/code>&nbsp;if the player hit&nbsp;<code>10<\/code>&nbsp;pins in any of the previous two turns.<\/li><li>Otherwise, It is&nbsp;<code>x<sub>i<\/sub><\/code>.<\/li><\/ul>\n\n\n\n<p>The score of the player is the sum of the values of their&nbsp;<code>n<\/code>&nbsp;turns.<\/p>\n\n\n\n<p>Return<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1<\/code>&nbsp;<em>if the score of player 1 is more than the score of player 2,<\/em><\/li><li><code>2<\/code>&nbsp;<em>if the score of player 2 is more than the score of player 1, and<\/em><\/li><li><code>0<\/code>&nbsp;<em>in case of a draw.<\/em><\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> player1 = [4,10,7,9], player2 = [6,5,2,3]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The score of player1 is 4 + 10 + 2*7 + 2*9 = 46.\nThe score of player2 is 6 + 5 + 2 + 3 = 16.\nScore of player1 is more than the score of player2, so, player1 is the winner, and the answer is 1.\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> player1 = [3,5,7,6], player2 = [8,10,10,2]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The score of player1 is 3 + 5 + 7 + 6 = 21.\nThe score of player2 is 8 + 10 + 2*10 + 2*2 = 42.\nScore of player2 is more than the score of player1, so, player2 is the winner, and the answer is 2.\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> player1 = [2,3], player2 = [4,1]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> The score of player1 is 2 + 3 = 5\nThe score of player2 is 4 + 1 = 5\nThe score of player1 equals to the score of player2, so, there is a draw, and the answer is 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == player1.length == player2.length<\/code><\/li><li><code>1 &lt;= n &lt;= 1000<\/code><\/li><li><code>0 &lt;= player1[i], player2[i] &lt;= 10<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>We can write a function to compute the score of a player.<\/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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int isWinner(vector<int>& player1, vector<int>& player2) {\n    auto score = [] (const vector<int>& player) {\n      int sum = 0;\n      int twice = 0;\n      for (int x : player) {        \n        sum += twice-- > 0 ? x * 2 : x;\n        if (x == 10) twice = 2;\n      }\n      return sum;\n    };\n    int s1 = score(player1);\n    int s2 = score(player2);\n    if (s1 == s2) return 0;\n    return s1 > s2 ? 1 : 2;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two&nbsp;0-indexed&nbsp;integer arrays&nbsp;player1&nbsp;and&nbsp;player2, that represent the number of pins that player 1 and player 2 hit in a bowling game, respectively. The bowling&#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":[222,179],"class_list":["post-10036","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10036","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=10036"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10036\/revisions"}],"predecessor-version":[{"id":10038,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10036\/revisions\/10038"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}