{"id":3947,"date":"2018-09-13T21:49:28","date_gmt":"2018-09-14T04:49:28","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3947"},"modified":"2018-09-13T21:52:37","modified_gmt":"2018-09-14T04:52:37","slug":"leetcode-900-rle-iterator","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-900-rle-iterator\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 900. RLE Iterator"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Write an iterator that iterates through a run-length encoded sequence.<\/p>\n<p>The iterator is initialized by\u00a0<code>RLEIterator(int[] A)<\/code>, where\u00a0<code>A<\/code>\u00a0is a run-length encoding of some\u00a0sequence.\u00a0 More specifically,\u00a0for all even\u00a0<code>i<\/code>,\u00a0<code>A[i]<\/code>\u00a0tells us the number of times that the non-negative integer value\u00a0<code>A[i+1]<\/code>\u00a0is repeated in the sequence.<\/p>\n<p>The iterator supports one function:\u00a0<code>next(int n)<\/code>, which exhausts the next\u00a0<code>n<\/code>\u00a0elements\u00a0(<code>n &gt;= 1<\/code>) and returns the last element exhausted in this way.\u00a0 If there is no element left to exhaust,\u00a0<code>next<\/code>\u00a0returns\u00a0<code>-1<\/code>\u00a0instead.<\/p>\n<p>For example, we start with\u00a0<code>A = [3,8,0,9,2,5]<\/code>, which is a run-length encoding of the sequence\u00a0<code>[8,8,8,5,5]<\/code>.\u00a0 This is because the sequence can be read as\u00a0&#8220;three eights, zero nines, two fives&#8221;.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<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>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">[null,8,8,5,-1]<\/span>\r\n<strong>Explanation: <\/strong>\r\nRLEIterator is initialized with RLEIterator([3,8,0,9,2,5]).\r\nThis maps to the sequence [8,8,8,5,5].\r\nRLEIterator.next is then called 4 times:\r\n\r\n.next(2) exhausts 2 terms of the sequence, returning 8.  The remaining sequence is now [8, 5, 5].\r\n\r\n.next(1) exhausts 1 term of the sequence, returning 8.  The remaining sequence is now [5, 5].\r\n\r\n.next(1) exhausts 1 term of the sequence, returning 5.  The remaining sequence is now [5].\r\n\r\n.next(2) exhausts 2 terms, returning -1.  This is because the first term exhausted was 5,\r\nbut the second term did not exist.  Since the last term exhausted does not exist, we return -1.\r\n\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>0 &lt;= A.length &lt;= 1000<\/code><\/li>\n<li><code>A.length<\/code>\u00a0is an even integer.<\/li>\n<li><code>0 &lt;= A[i] &lt;= 10^9<\/code><\/li>\n<li>There are at most\u00a0<code>1000<\/code>\u00a0calls to\u00a0<code>RLEIterator.next(int n)<\/code>\u00a0per test case.<\/li>\n<li>Each call to\u00a0<code>RLEIterator.next(int n)<\/code>\u00a0will have\u00a0<code>1 &lt;= n &lt;= 10^9<\/code>.<\/li>\n<\/ol>\n<h1><strong>Solution: Simulation<\/strong><\/h1>\n<p>Time complexity: O(|A|)<\/p>\n<p>Space complexity: O(|A|)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua, 4 ms\r\nclass RLEIterator {\r\npublic:\r\n  RLEIterator(vector&lt;int&gt; A): A_(std::move(A)), i_(0) {}\r\n\r\n  int next(int n) {\r\n    while (n &amp;&amp; i_ &lt; A_.size()) {\r\n      if (n &gt;= A_[i_]) {\r\n        n -= A_[i_];\r\n        i_ += 2;\r\n        if (n == 0) return A_[i_ - 1];\r\n      } else {\r\n        A_[i_] -= n;\r\n        return A_[i_ + 1];\r\n      }        \r\n    }\r\n    return -1;\r\n  }\r\nprivate:\r\n  int i_;\r\n  vector&lt;int&gt; A_;\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:java decode:true\">\/\/ Author: Huahua, 73 ms\r\nclass RLEIterator {\r\n  private int[] A;\r\n  private int i;\r\n  public RLEIterator(int[] A) {\r\n    this.A = A;\r\n    this.i = 0;\r\n  }\r\n\r\n  public int next(int n) {\r\n    while (n &gt;= 0 &amp;&amp; i &lt; A.length) {\r\n      if (n &gt;= A[i]) {\r\n        n -= A[i];\r\n        i += 2;\r\n        if (n == 0) return A[i - 1];\r\n      } else {\r\n        A[i] -= n;\r\n        return A[i + 1];\r\n      }\r\n    }\r\n    return -1;\r\n  }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:r decode:true\"># Author: Huahua, 44 ms\r\nclass RLEIterator:\r\n  def __init__(self, A):\r\n    self.A = A\r\n    self.i = 0\r\n\r\n  def next(self, n):\r\n    while n != 0 and self.i &lt; len(self.A):\r\n      if n &gt;= self.A[self.i]:\r\n        n -= self.A[self.i]\r\n        self.i += 2\r\n        if n == 0: return self.A[self.i - 1]\r\n      else:\r\n        self.A[self.i] -= n\r\n        return self.A[self.i + 1]\r\n    return -1<\/pre>\n<p>&nbsp;<\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized by\u00a0RLEIterator(int[] A), where\u00a0A\u00a0is a run-length encoding of some\u00a0sequence.\u00a0 More specifically,\u00a0for&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[405],"class_list":["post-3947","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-rle","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3947"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3947\/revisions"}],"predecessor-version":[{"id":3951,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3947\/revisions\/3951"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}