{"id":4383,"date":"2018-11-29T21:45:43","date_gmt":"2018-11-30T05:45:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4383"},"modified":"2018-11-29T21:52:47","modified_gmt":"2018-11-30T05:52:47","slug":"leetcode-948-bag-of-tokens","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-948-bag-of-tokens\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 948. Bag of Tokens"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>You have an initial power\u00a0<code>P<\/code>, an initial score of\u00a0<code>0<\/code>\u00a0points, and a bag of tokens.<\/p>\n<p>Each token can be used at most once, has a value\u00a0<code>token[i]<\/code>, and has potentially two ways to use it.<\/p>\n<ul>\n<li>If we have at least\u00a0<code>token[i]<\/code>\u00a0power, we may play the token face up, losing\u00a0<code>token[i]<\/code>\u00a0power, and gaining\u00a0<code>1<\/code>\u00a0point.<\/li>\n<li>If we have at least\u00a0<code>1<\/code>\u00a0point, we may play the token face down, gaining\u00a0<code>token[i]<\/code>power, and losing\u00a0<code>1<\/code>\u00a0point.<\/li>\n<\/ul>\n<p>Return the largest number of points we can have after playing any number of tokens.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong>tokens = <span id=\"example-input-1-1\">[100]<\/span>, P = <span id=\"example-input-1-2\">50<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">0<\/span>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>tokens = <span id=\"example-input-2-1\">[100,200]<\/span>, P = <span id=\"example-input-2-2\">150<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">1<\/span>\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>tokens = <span id=\"example-input-3-1\">[100,200,300,400]<\/span>, P = <span id=\"example-input-3-2\">200<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">2<\/span>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>tokens.length &lt;= 1000<\/code><\/li>\n<li><code>0 &lt;= tokens[i] &lt; 10000<\/code><\/li>\n<li><code>0 &lt;= P &lt; 10000<\/code><\/li>\n<\/ol>\n<h1><strong>Solution: Greedy + Two Pointers<\/strong><\/h1>\n<p>Sort the tokens, gain points from the low end gain power from the high end.<\/p>\n<p>Time complexity: O(nlogn)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua, Running time: 8 ms\r\nclass Solution {\r\npublic:\r\n  int bagOfTokensScore(vector&lt;int&gt;&amp; tokens, int P) {\r\n    sort(begin(tokens), end(tokens));\r\n    int points = 0;\r\n    int ans = 0;\r\n    int i = 0;\r\n    int j = tokens.size() - 1;\r\n    \r\n    while (i &lt;= j)\r\n      if (P &gt;= tokens[i]) {\r\n        P -= tokens[i++];\r\n        ans = max(ans, ++points);        \r\n      } else if (points &gt; 0) {\r\n        P += tokens[j--];\r\n        --points;\r\n      } else {\r\n        break;\r\n      }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem You have an initial power\u00a0P, an initial score of\u00a00\u00a0points, and a bag of tokens. Each token can be used at most once, has a&#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,176],"tags":[88,177,438],"class_list":["post-4383","post","type-post","status-publish","format-standard","hentry","category-greedy","category-two-pointers","tag-greedy","tag-medium","tag-tow-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4383","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=4383"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4383\/revisions"}],"predecessor-version":[{"id":4388,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4383\/revisions\/4388"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}