{"id":59,"date":"2018-11-26T19:07:13","date_gmt":"2018-11-26T19:07:13","guid":{"rendered":"https:\/\/andybase.com\/?p=59"},"modified":"2018-11-27T14:32:27","modified_gmt":"2018-11-27T14:32:27","slug":"948-bag-of-tokens","status":"publish","type":"post","link":"https:\/\/andybase.com\/?p=59","title":{"rendered":"948 Bag of Tokens"},"content":{"rendered":"\n<p>Link: <a href=\"https:\/\/leetcode.com\/problems\/bag-of-tokens\/\">Bag of Tokens<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Thought<\/h2>\n\n\n\n<p>For this question, I find we could do 2 things: spend any cost form the tokens, and get 1 point. Or, spend 1 point, get any power from the tokens. So it turns to be pretty clear that I will firstly sort all tokens by value, and trying to get as much point as possible by flipping from left(smaller) side. If I can not flip any more, try to gain as much power as possible(AKA from right side). <br><br>And during the same time, keep tracking the maximum points I can get.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code<\/h2>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code brush: python; notranslate\">class Solution(object):\n    def bagOfTokensScore(self, tokens, P):\n        \"\"\"\n        :type tokens: List[int]\n        :type P: int\n        :rtype: int\n        \"\"\"\n        tokens.sort()\n        init = 0\n        maxx = 0\n        lindx = 0\n        rindx = len(tokens) - 1\n        while lindx &lt;= rindx:\n            while lindx &lt;= rindx and tokens[lindx] &lt;= P:\n                P -= tokens[lindx]\n                init += 1\n                lindx += 1\n            maxx = max(maxx, init)\n            if init == 0:\n                break\n            if lindx &lt;= rindx:\n                init -= 1\n                P += tokens[rindx]\n                rindx -= 1\n        return maxx<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Runtime<\/h2>\n\n\n\n<p>Time: O(NlogN)<\/p>\n\n\n\n<p>Space: O(1)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Link: Bag of Tokens Thought For this question, I find we could do 2 things: spend any cost form the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[2],"tags":[4,3],"class_list":["post-59","post","type-post","status-publish","format-standard","hentry","category-leetcode","tag-contest","tag-leetcode"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paWPni-X","_links":{"self":[{"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/posts\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/andybase.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=59"}],"version-history":[{"count":2,"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":74,"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions\/74"}],"wp:attachment":[{"href":"https:\/\/andybase.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andybase.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andybase.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}