{"id":37,"date":"2017-03-24T22:38:38","date_gmt":"2017-03-25T05:38:38","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=37"},"modified":"2018-09-06T12:39:06","modified_gmt":"2018-09-06T19:39:06","slug":"leetcode-486-predict-the-winner","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/leetcode\/leetcode-486-predict-the-winner\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 486. Predict the Winner"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 486. Predict the Winner - \u5237\u9898\u627e\u5de5\u4f5c EP185\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/g5wLHFTodm0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins.<\/p>\n<p>Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score.<\/p>\n<p><strong>Example 1:<\/strong><br \/>\nInput: [1, 5, 2]<br \/>\nOutput: False<br \/>\nExplanation: Initially, player 1 can choose between 1 and 2.<br \/>\nIf he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).<br \/>\nSo, final score of player 1 is 1 + 2 = 3, and player 2 is 5.<br \/>\nHence, player 1 will never be the winner and you need to return False.<\/p>\n<p><strong>Example 2:<\/strong><br \/>\nInput: [1, 5, 233, 7]<br \/>\nOutput: True<br \/>\nExplanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.<br \/>\nFinally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.<br \/>\nNote:<br \/>\n1 &lt;= length of the array &lt;= 20.<br \/>\nAny scores in the given array are non-negative integers and will not exceed 10,000,000.<br \/>\nIf the scores of both players are equal, then player 1 is still the winner.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2768\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/03\/486-ep185.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/03\/486-ep185.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/03\/486-ep185-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/03\/486-ep185-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<h1><strong>Solution 1: Recursion<\/strong><\/h1>\n<p>Time complexity: O(2^n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 67 ms\r\nclass Solution {\r\npublic:\r\n  bool PredictTheWinner(vector&lt;int&gt;&amp; nums) {    \r\n    return getScore(nums, 0, nums.size() - 1) &gt;= 0;\r\n  }\r\nprivate:  \r\n  \/\/ Max diff (my_score - op_score) of subarray nums[l] ~ nums[r].\r\n  int getScore(vector&lt;int&gt;&amp; nums, int l, int r) {\r\n    if (l == r) return nums[l];\r\n    return max(nums[l] - getScore(nums, l + 1, r), \r\n               nums[r] - getScore(nums, l, r - 1));    \r\n  }\r\n};<\/pre>\n<h1><strong>Solution 2: Recursion + Memoization<\/strong><\/h1>\n<p>Time complexity: O(n^2)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  bool PredictTheWinner(vector&lt;int&gt;&amp; nums) {\r\n    m_ = vector&lt;vector&lt;int&gt;&gt;(nums.size(), vector&lt;int&gt;(nums.size(), INT_MIN));\r\n    return getScore(nums, 0, nums.size() - 1) &gt;= 0;\r\n  }\r\nprivate:\r\n  vector&lt;vector&lt;int&gt;&gt; m_;\r\n  \/\/ Max diff (my_score - op_score) of subarray nums[l] ~ nums[r].\r\n  int getScore(vector&lt;int&gt;&amp; nums, int l, int r) {\r\n    if (l == r) return nums[l];    \r\n    if (m_[l][r] != INT_MIN) return m_[l][r];\r\n    m_[l][r] = max(nums[l] - getScore(nums, l + 1, r), \r\n                   nums[r] - getScore(nums, l, r - 1));\r\n    return m_[l][r];\r\n  }    \r\n};<\/pre>\n<p>DP version<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  bool PredictTheWinner(vector&lt;int&gt;&amp; nums) {\r\n    const int n = nums.size();\r\n    vector&lt;vector&lt;int&gt;&gt; dp(n, vector&lt;int&gt;(n, INT_MIN));\r\n    for (int i = 0; i &lt; n; ++i)\r\n      dp[i][i] = nums[i];\r\n    for (int l = 2; l &lt;= n; ++l)\r\n      for (int i = 0; i &lt;= n - l; ++i) {\r\n        int j = i + l - 1;\r\n        dp[i][j] = max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]);\r\n      }\r\n    return dp[0][n - 1] &gt;= 0;\r\n  }\r\n};<\/pre>\n<h1><strong>Related Problem<\/strong><\/h1>\n<ul>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-877-stone-game\/\">\u82b1\u82b1\u9171 LeetCode 877. Stone Game<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,3],"tags":[26,27,177,25,17],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","category-leetcode","tag-dynamic-programming","tag-game","tag-medium","tag-min-max","tag-recursion","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/37","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=37"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":3882,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/37\/revisions\/3882"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}