<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>tow pointers Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/tow-pointers/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/tow-pointers/</link>
	<description></description>
	<lastBuildDate>Fri, 30 Nov 2018 05:52:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>tow pointers Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/tow-pointers/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 948. Bag of Tokens</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-948-bag-of-tokens/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-948-bag-of-tokens/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 30 Nov 2018 05:45:43 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[Two pointers]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[tow pointers]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4383</guid>

					<description><![CDATA[<p>Problem You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be used at most once, has a&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-948-bag-of-tokens/">花花酱 LeetCode 948. Bag of Tokens</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>You have an initial power <code>P</code>, an initial score of <code>0</code> points, and a bag of tokens.</p>
<p>Each token can be used at most once, has a value <code>token[i]</code>, and has potentially two ways to use it.</p>
<ul>
<li>If we have at least <code>token[i]</code> power, we may play the token face up, losing <code>token[i]</code> power, and gaining <code>1</code> point.</li>
<li>If we have at least <code>1</code> point, we may play the token face down, gaining <code>token[i]</code>power, and losing <code>1</code> point.</li>
</ul>
<p>Return the largest number of points we can have after playing any number of tokens.</p>
<p><strong>Example 1:</strong></p>
<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>
<strong>Output: </strong><span id="example-output-1">0</span>
</pre>
<p><strong>Example 2:</strong></p>
<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>
<strong>Output: </strong><span id="example-output-2">1</span>
</pre>
<p><strong>Example 3:</strong></p>
<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>
<strong>Output: </strong><span id="example-output-3">2</span>
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>tokens.length &lt;= 1000</code></li>
<li><code>0 &lt;= tokens[i] &lt; 10000</code></li>
<li><code>0 &lt;= P &lt; 10000</code></li>
</ol>
<h1><strong>Solution: Greedy + Two Pointers</strong></h1>
<p>Sort the tokens, gain points from the low end gain power from the high end.</p>
<p>Time complexity: O(nlogn)</p>
<p>Space complexity: O(1)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, Running time: 8 ms
class Solution {
public:
  int bagOfTokensScore(vector&lt;int&gt;&amp; tokens, int P) {
    sort(begin(tokens), end(tokens));
    int points = 0;
    int ans = 0;
    int i = 0;
    int j = tokens.size() - 1;
    
    while (i &lt;= j)
      if (P &gt;= tokens[i]) {
        P -= tokens[i++];
        ans = max(ans, ++points);        
      } else if (points &gt; 0) {
        P += tokens[j--];
        --points;
      } else {
        break;
      }
    return ans;
  }
};</pre><p></div></div></p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-948-bag-of-tokens/">花花酱 LeetCode 948. Bag of Tokens</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/greedy/leetcode-948-bag-of-tokens/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
