<?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>delection Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/delection/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/delection/</link>
	<description></description>
	<lastBuildDate>Sat, 14 Nov 2020 18:51:55 +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>delection Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/delection/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1653. Minimum Deletions to Make String Balanced</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1653-minimum-deletions-to-make-string-balanced/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1653-minimum-deletions-to-make-string-balanced/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 14 Nov 2020 18:50:48 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[delection]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7648</guid>

					<description><![CDATA[<p>You are given a string&#160;s&#160;consisting only of characters&#160;'a'&#160;and&#160;'b'​​​​. You can delete any number of characters in&#160;s&#160;to make&#160;s&#160;balanced.&#160;s&#160;is&#160;balanced&#160;if there is no pair of indices&#160;(i,j)&#160;such that&#160;i &#60;&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1653-minimum-deletions-to-make-string-balanced/">花花酱 LeetCode 1653. Minimum Deletions to Make String Balanced</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given a string&nbsp;<code>s</code>&nbsp;consisting only of characters&nbsp;<code>'a'</code>&nbsp;and&nbsp;<code>'b'</code>​​​​.</p>



<p>You can delete any number of characters in&nbsp;<code>s</code>&nbsp;to make&nbsp;<code>s</code>&nbsp;<strong>balanced</strong>.&nbsp;<code>s</code>&nbsp;is&nbsp;<strong>balanced</strong>&nbsp;if there is no pair of indices&nbsp;<code>(i,j)</code>&nbsp;such that&nbsp;<code>i &lt; j</code>&nbsp;and&nbsp;<code>s[i] = 'b'</code>&nbsp;and&nbsp;<code>s[j]= 'a'</code>.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>minimum</strong>&nbsp;number of deletions needed to make&nbsp;</em><code>s</code><em>&nbsp;<strong>balanced</strong></em>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aababbab" -&gt; "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aababbab" -&gt; "aabbbb").
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li><li><code>s[i]</code>&nbsp;is&nbsp;<code>'a'</code>&nbsp;or&nbsp;<code>'b'</code>​​.</li></ul>



<h2><strong>Solution: DP</strong></h2>



<p>dp[i][0] := min # of dels to make s[0:i] all &#8216;a&#8217;s;<br>dp[i][1] := min # of dels to make s[0:i] ends with 0 or mode &#8216;b&#8217;s</p>



<p>if s[i-1] == &#8216;a&#8217;: <br>  dp[i + 1][0] = dp[i][0],  dp[i + 1][1] = min(dp[i + 1][0], dp[i][1] + 1)<br>else:<br>  dp[i + 1][0] = dp[i][0] + 1, dp[i + 1][1] = dp[i][1]</p>



<p>Time complexity: O(n)<br>Space complexity: O(n) -&gt; O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minimumDeletions(string s) {
    // const int n = s.length();
    // dp[i][0] := min # of dels to make s[0:i] all 'a's;
    // dp[i][1] := min # of dels to make s[0:i] ends with 0+ 'b's
    // vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(2));
    // for (int i = 0; i &lt; n; ++i) {
    //   if (s[i] == 'a') {
    //     dp[i + 1][0] = dp[i][0];
    //     dp[i + 1][1] = min(dp[i + 1][0], dp[i][1] + 1);
    //   } else {
    //     dp[i + 1][0] = dp[i][0] + 1;
    //     dp[i + 1][1] = dp[i][1];
    //   }
    // }
    // return min(dp[n][0], dp[n][1]);
    int a = 0, b = 0;
    for (char c : s) {
      if (c == 'a') b = min(a, b + 1); 
      else ++a;
    }
    return min(a, b);
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1653-minimum-deletions-to-make-string-balanced/">花花酱 LeetCode 1653. Minimum Deletions to Make String Balanced</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/dynamic-programming/leetcode-1653-minimum-deletions-to-make-string-balanced/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
