{"id":8083,"date":"2021-02-07T08:30:33","date_gmt":"2021-02-07T16:30:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8083"},"modified":"2021-02-07T09:01:02","modified_gmt":"2021-02-07T17:01:02","slug":"leetcode-1753-maximum-score-from-removing-stones","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1753-maximum-score-from-removing-stones\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1753. Maximum Score From Removing Stones"},"content":{"rendered":"\n<p>You are playing a solitaire game with&nbsp;<strong>three piles<\/strong>&nbsp;of stones of sizes&nbsp;<code>a<\/code>\u200b\u200b\u200b\u200b\u200b\u200b,&nbsp;<code>b<\/code>,\u200b\u200b\u200b\u200b\u200b\u200b and&nbsp;<code>c<\/code>\u200b\u200b\u200b\u200b\u200b\u200b respectively. Each turn you choose two&nbsp;<strong>different non-empty&nbsp;<\/strong>piles, take one stone from each, and add&nbsp;<code>1<\/code>&nbsp;point to your score. The game stops when there are&nbsp;<strong>fewer than two non-empty<\/strong>&nbsp;piles (meaning there are no more available moves).<\/p>\n\n\n\n<p>Given three integers&nbsp;<code>a<\/code>\u200b\u200b\u200b\u200b\u200b,&nbsp;<code>b<\/code>,\u200b\u200b\u200b\u200b\u200b and&nbsp;<code>c<\/code>\u200b\u200b\u200b\u200b\u200b, return&nbsp;<em>the<\/em>&nbsp;<strong><em>maximum<\/em>&nbsp;<\/strong><em><strong>score<\/strong>&nbsp;you can get.<\/em><\/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> a = 2, b = 4, c = 6\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> The starting state is (2, 4, 6). One optimal set of moves is:\n- Take from 1st and 3rd piles, state is now (1, 4, 5)\n- Take from 1st and 3rd piles, state is now (0, 4, 4)\n- Take from 2nd and 3rd piles, state is now (0, 3, 3)\n- Take from 2nd and 3rd piles, state is now (0, 2, 2)\n- Take from 2nd and 3rd piles, state is now (0, 1, 1)\n- Take from 2nd and 3rd piles, state is now (0, 0, 0)\nThere are fewer than two non-empty piles, so the game ends. Total: 6 points.\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> a = 4, b = 4, c = 6\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> The starting state is (4, 4, 6). One optimal set of moves is:\n- Take from 1st and 2nd piles, state is now (3, 3, 6)\n- Take from 1st and 3rd piles, state is now (2, 3, 5)\n- Take from 1st and 3rd piles, state is now (1, 3, 4)\n- Take from 1st and 3rd piles, state is now (0, 3, 3)\n- Take from 2nd and 3rd piles, state is now (0, 2, 2)\n- Take from 2nd and 3rd piles, state is now (0, 1, 1)\n- Take from 2nd and 3rd piles, state is now (0, 0, 0)\nThere are fewer than two non-empty piles, so the game ends. Total: 7 points.\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> a = 1, b = 8, c = 8\n<strong>Output:<\/strong> 8\n<strong>Explanation:<\/strong> One optimal set of moves is to take from the 2nd and 3rd piles for 8 turns until they are empty.\nAfter that, there are fewer than two non-empty piles, so the game ends.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= a, b, c &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Greedy<\/strong><\/h2>\n\n\n\n<p>Take two stones (one each) from the largest two piles, until one is empty.<\/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 maximumScore(int a, int b, int c) {\n    array<int, 3> s{a,b,c};\n    sort(begin(s), end(s));\n    int ans = 0;\n    while (s[1]-- && s[2]--) {      \n      ++ans;\n      sort(begin(s), end(s));\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Math<\/strong><\/h2>\n\n\n\n<p>First, let&#8217;s assuming a &lt;= b &lt;= c. <br>There are two conditions:<br>1. a + b &lt;= c, we can pair c with a first and then b. Total pairs is (a + b + (a + b)) \/ 2<br>2. a + b &gt; c, we can pair c with a, b &#8220;evenly&#8221;, and then pair a with b, total pairs is (a + b + c) \/ 2<\/p>\n\n\n\n<p>ans = (a + b + min(a + b, c)) \/ 2<\/p>\n\n\n\n<p>Time complexity: O(1)<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 maximumScore(int a, int b, int c) {\n    array<int, 3> s{a,b,c};\n    sort(begin(s), end(s));\n    return (s[0] + s[1] + min(s[0] + s[1], s[2])) \/ 2;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are playing a solitaire game with&nbsp;three piles&nbsp;of stones of sizes&nbsp;a\u200b\u200b\u200b\u200b\u200b\u200b,&nbsp;b,\u200b\u200b\u200b\u200b\u200b\u200b and&nbsp;c\u200b\u200b\u200b\u200b\u200b\u200b respectively. Each turn you choose two&nbsp;different non-empty&nbsp;piles, take one stone from each, and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,31,177],"class_list":["post-8083","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8083","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=8083"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8083\/revisions"}],"predecessor-version":[{"id":8086,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8083\/revisions\/8086"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}