<?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>RLE Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/rle/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/rle/</link>
	<description></description>
	<lastBuildDate>Mon, 18 May 2020 01:40:39 +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>RLE Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/rle/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1446. Consecutive Characters</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1446-consecutive-characters/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1446-consecutive-characters/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 18 May 2020 01:39:55 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[RLE]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6779</guid>

					<description><![CDATA[<p>Given a string&#160;s, the power of the string is the maximum length of a non-empty substring that&#160;contains only one unique character. Return&#160;the power&#160;of the string.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1446-consecutive-characters/">花花酱 LeetCode 1446. Consecutive Characters</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>Given a string&nbsp;<code>s</code>, the power of the string is the maximum length of a non-empty substring that&nbsp;contains only one unique character.</p>



<p>Return&nbsp;<em>the power</em>&nbsp;of the string.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The substring "ee" is of length 2 with the character 'e' only.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "abbcccddddeeeeedcba"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The substring "eeeee" is of length 5 with the character 'e' only.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "triplepillooooow"
<strong>Output:</strong> 5
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "hooraaaaaaaaaaay"
<strong>Output:</strong> 11
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "tourist"
<strong>Output:</strong> 1
</pre>



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



<ul><li><code>1 &lt;= s.length &lt;= 500</code></li><li><code>s</code>&nbsp;contains only lowercase English letters.</li></ul>



<h2><strong>Solution: Run length encoding?</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: 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 maxPower(string s) {
    char p = '?';
    int ans = 0;
    int cur = 0;
    for (char c : s) {
      if (c != p) cur = 0;      
      p = c;
      ans = max(ans, ++cur);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1446-consecutive-characters/">花花酱 LeetCode 1446. Consecutive Characters</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/string/leetcode-1446-consecutive-characters/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 900. RLE Iterator</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-900-rle-iterator/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-900-rle-iterator/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 14 Sep 2018 04:49:28 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[RLE]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3947</guid>

					<description><![CDATA[<p>Problem Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized by RLEIterator(int[] A), where A is a run-length encoding of some sequence.  More specifically, for&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-900-rle-iterator/">花花酱 LeetCode 900. RLE Iterator</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>Write an iterator that iterates through a run-length encoded sequence.</p>
<p>The iterator is initialized by <code>RLEIterator(int[] A)</code>, where <code>A</code> is a run-length encoding of some sequence.  More specifically, for all even <code>i</code>, <code>A[i]</code> tells us the number of times that the non-negative integer value <code>A[i+1]</code> is repeated in the sequence.</p>
<p>The iterator supports one function: <code>next(int n)</code>, which exhausts the next <code>n</code> elements (<code>n &gt;= 1</code>) and returns the last element exhausted in this way.  If there is no element left to exhaust, <code>next</code> returns <code>-1</code> instead.</p>
<p>For example, we start with <code>A = [3,8,0,9,2,5]</code>, which is a run-length encoding of the sequence <code>[8,8,8,5,5]</code>.  This is because the sequence can be read as &#8220;three eights, zero nines, two fives&#8221;.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">["RLEIterator","next","next","next","next"]</span>, <span id="example-input-1-2">[[[3,8,0,9,2,5]],[2],[1],[1],[2]]</span>
<strong>Output: </strong><span id="example-output-1">[null,8,8,5,-1]</span>
<strong>Explanation: </strong>
RLEIterator is initialized with RLEIterator([3,8,0,9,2,5]).
This maps to the sequence [8,8,8,5,5].
RLEIterator.next is then called 4 times:

.next(2) exhausts 2 terms of the sequence, returning 8.  The remaining sequence is now [8, 5, 5].

.next(1) exhausts 1 term of the sequence, returning 8.  The remaining sequence is now [5, 5].

.next(1) exhausts 1 term of the sequence, returning 5.  The remaining sequence is now [5].

.next(2) exhausts 2 terms, returning -1.  This is because the first term exhausted was 5,
but the second term did not exist.  Since the last term exhausted does not exist, we return -1.

</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>0 &lt;= A.length &lt;= 1000</code></li>
<li><code>A.length</code> is an even integer.</li>
<li><code>0 &lt;= A[i] &lt;= 10^9</code></li>
<li>There are at most <code>1000</code> calls to <code>RLEIterator.next(int n)</code> per test case.</li>
<li>Each call to <code>RLEIterator.next(int n)</code> will have <code>1 &lt;= n &lt;= 10^9</code>.</li>
</ol>
<h1><strong>Solution: Simulation</strong></h1>
<p>Time complexity: O(|A|)</p>
<p>Space complexity: O(|A|)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 4 ms
class RLEIterator {
public:
  RLEIterator(vector&lt;int&gt; A): A_(std::move(A)), i_(0) {}

  int next(int n) {
    while (n &amp;&amp; i_ &lt; A_.size()) {
      if (n &gt;= A_[i_]) {
        n -= A_[i_];
        i_ += 2;
        if (n == 0) return A_[i_ - 1];
      } else {
        A_[i_] -= n;
        return A_[i_ + 1];
      }        
    }
    return -1;
  }
private:
  int i_;
  vector&lt;int&gt; A_;
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 73 ms
class RLEIterator {
  private int[] A;
  private int i;
  public RLEIterator(int[] A) {
    this.A = A;
    this.i = 0;
  }

  public int next(int n) {
    while (n &gt;= 0 &amp;&amp; i &lt; A.length) {
      if (n &gt;= A[i]) {
        n -= A[i];
        i += 2;
        if (n == 0) return A[i - 1];
      } else {
        A[i] -= n;
        return A[i + 1];
      }
    }
    return -1;
  }
}</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag"># Author: Huahua, 44 ms
class RLEIterator:
  def __init__(self, A):
    self.A = A
    self.i = 0

  def next(self, n):
    while n != 0 and self.i &lt; len(self.A):
      if n &gt;= self.A[self.i]:
        n -= self.A[self.i]
        self.i += 2
        if n == 0: return self.A[self.i - 1]
      else:
        self.A[self.i] -= n
        return self.A[self.i + 1]
    return -1</pre><p>&nbsp;</p>
<p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-900-rle-iterator/">花花酱 LeetCode 900. RLE Iterator</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/simulation/leetcode-900-rle-iterator/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
