<?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>DI Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/di/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/di/</link>
	<description></description>
	<lastBuildDate>Sun, 18 Nov 2018 17:48:18 +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>DI Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/di/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 942. DI String Match</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-942-di-string-match/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-942-di-string-match/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 18 Nov 2018 17:48:08 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[DI]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[greedy]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4334</guid>

					<description><![CDATA[<p>Problem Given a string S that only contains &#8220;I&#8221; (increase) or &#8220;D&#8221; (decrease), let N = S.length. Return any permutation A of [0, 1, ..., N] such that for all i = 0, ..., N-1: If S[i] == "I",&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-942-di-string-match/">花花酱 LeetCode 942. DI String Match</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>Given a string <code>S</code> that <strong>only</strong> contains &#8220;I&#8221; (increase) or &#8220;D&#8221; (decrease), let <code>N = S.length</code>.</p>
<p>Return <strong>any</strong> permutation <code>A</code> of <code>[0, 1, ..., N]</code> such that for all <code>i = 0, ..., N-1</code>:</p>
<ul>
<li>If <code>S[i] == "I"</code>, then <code>A[i] &lt; A[i+1]</code></li>
<li>If <code>S[i] == "D"</code>, then <code>A[i] &gt; A[i+1]</code></li>
</ul>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">"IDID"</span>
<strong>Output: </strong><span id="example-output-1">[0,4,1,3,2]</span>
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-2-1">"III"</span>
<strong>Output: </strong><span id="example-output-2">[0,1,2,3]</span>
</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-3-1">"DDI"</span>
<strong>Output: </strong><span id="example-output-3">[3,2,0,1]</span></pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= S.length &lt;= 10000</code></li>
<li><code>S</code> only contains characters <code>"I"</code> or <code>"D"</code>.</li>
</ol>
<h1><strong>Solution: Greedy</strong></h1>
<p>&#8220;I&#8221; -&gt; use the smallest possible number</p>
<p>&#8220;D&#8221; -&gt; use the largest possible number</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 44 ms
class Solution {
public:
  vector&lt;int&gt; diStringMatch(string S) {
    const int n = S.length();
    vector&lt;int&gt; ans;
    int lo = 0;
    int hi = n;
    for (char c : S) {
      if (c == 'I')
        ans.push_back(lo++);
      else
        ans.push_back(hi--);
    }
    ans.push_back(lo);
    return ans;
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-942-di-string-match/">花花酱 LeetCode 942. DI String Match</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-942-di-string-match/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
