<?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>skiplist Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/skiplist/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/skiplist/</link>
	<description></description>
	<lastBuildDate>Sat, 07 Nov 2020 06:54:44 +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>skiplist Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/skiplist/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1206. Design Skiplist</title>
		<link>https://zxi.mytechroad.com/blog/desgin/leetcode-1206-design-skiplist/</link>
					<comments>https://zxi.mytechroad.com/blog/desgin/leetcode-1206-design-skiplist/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 06 Nov 2020 21:19:24 +0000</pubDate>
				<category><![CDATA[Desgin]]></category>
		<category><![CDATA[data structure]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[skiplist]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7606</guid>

					<description><![CDATA[<p>Design a Skiplist without using any built-in libraries. A Skiplist is a data structure that takes&#160;O(log(n)) time&#160;to&#160;add,&#160;erase&#160;and&#160;search. Comparing with treap and red-black tree which has&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/desgin/leetcode-1206-design-skiplist/">花花酱 LeetCode 1206. Design Skiplist</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1206. Design Skiplist - 刷题找工作 EP367" width="500" height="281" src="https://www.youtube.com/embed/783qX31AN08?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Design a Skiplist without using any built-in libraries.</p>



<p><em>A Skiplist is a data structure that takes&nbsp;O(log(n)) time&nbsp;to&nbsp;<code>add</code>,&nbsp;<code>erase</code>&nbsp;and&nbsp;<code>search</code>. Comparing with treap and red-black tree which has the same function and performance, the code length of Skiplist can be&nbsp;comparatively short and the idea behind Skiplists are just simple linked lists.</em></p>



<p><em>For example:&nbsp;we have a Skiplist containing&nbsp;<code>[30,40,50,60,70,90]</code>&nbsp;and we want to add&nbsp;<code>80</code>&nbsp;and&nbsp;<code>45</code>&nbsp;into it. The&nbsp;Skiplist works this way:</em></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/09/27/1506_skiplist.gif" alt=""/></figure>



<p><br>Artyom Kalinin [CC BY-SA 3.0], via&nbsp;<a href="https://commons.wikimedia.org/wiki/File:Skip_list_add_element-en.gif" target="_blank" rel="noreferrer noopener">Wikimedia Commons</a></p>



<p><em>You can see there are many layers in the Skiplist. Each layer is a sorted linked list. With the help of the top layers,&nbsp;<code>add</code>&nbsp;,&nbsp;<code>erase</code>&nbsp;and&nbsp;<code>search&nbsp;</code>can be faster than O(n).&nbsp;It can be proven&nbsp;that the average time complexity for each operation is O(log(n)) and space complexity is O(n).</em></p>



<p>To be specific, your design should include these functions:</p>



<ul><li><code>bool search(int target)</code>&nbsp;: Return whether&nbsp;the&nbsp;<code>target</code>&nbsp;exists in the Skiplist&nbsp;or not.</li><li><code>void add(int num)</code>:&nbsp;Insert a value into the SkipList.&nbsp;</li><li><code>bool erase(int num)</code>: Remove a value in&nbsp;the Skiplist.&nbsp;If&nbsp;<code>num</code>&nbsp;does not exist in the Skiplist, do nothing and return false. If there exists multiple&nbsp;<code>num</code>&nbsp;values, removing&nbsp;any one of them is fine.</li></ul>



<p>See more about Skiplist :&nbsp;<a href="https://en.wikipedia.org/wiki/Skip_list" target="_blank" rel="noreferrer noopener">https://en.wikipedia.org/wiki/Skip_list</a></p>



<p>Note that duplicates may exist in the Skiplist, your code needs to handle this situation.</p>



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



<pre class="crayon-plain-tag">Skiplist skiplist = new Skiplist();

skiplist.add(1);
skiplist.add(2);
skiplist.add(3);
skiplist.search(0);   // return false.
skiplist.add(4);
skiplist.search(1);   // return true.
skiplist.erase(0);    // return false, 0 is not in skiplist.
skiplist.erase(1);    // return true.
skiplist.search(1);   // return false, 1 has already been erased.</pre>



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



<ul><li><code>0 &lt;= num, target&nbsp;&lt;= 20000</code></li><li>At most&nbsp;<code>50000</code>&nbsp;calls will be made to&nbsp;<code>search</code>,&nbsp;<code>add</code>, and&nbsp;<code>erase</code>.</li></ul>



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



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-1.png" alt="" class="wp-image-7610" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-2.png" alt="" class="wp-image-7611" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-3.png" alt="" class="wp-image-7612" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-4.png" alt="" class="wp-image-7613" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-4.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-4-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-4-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-5.png" alt="" class="wp-image-7614" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-5.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-5-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1206-ep367-5-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Skiplist {
public:
  Skiplist() { head_ = make_shared&lt;Node&gt;(); }

  bool search(int target) {
    for (auto node = head_; node; node = node-&gt;down) {
      while (node-&gt;next &amp;&amp; node-&gt;next-&gt;val &lt; target)
        node = node-&gt;next;
      if (node-&gt;next &amp;&amp; node-&gt;next-&gt;val == target)
        return true;
    }
    return false;
  }

  void add(int num) {
    stack&lt;shared_ptr&lt;Node&gt;&gt; s;
    shared_ptr&lt;Node&gt; down;
    bool insert = true;
    
    for (auto node = head_; node; node = node-&gt;down) {
      while (node-&gt;next &amp;&amp; node-&gt;next-&gt;val &lt; num)
        node = node-&gt;next;
      s.push(node);
    }
    
    while (!s.empty() &amp;&amp; insert) {
      auto cur = s.top(); s.pop();
      cur-&gt;next = make_shared&lt;Node&gt;(num, cur-&gt;next, down);
      down = cur-&gt;next;
      insert = rand() &amp; 1; // 50% chance
    }
    
    if (insert) // create a new layer.
      head_ = make_shared&lt;Node&gt;(-1, nullptr, head_);
  }

  bool erase(int num) {
    bool found = false;
    for (auto node = head_; node; node = node-&gt;down) {
      while (node-&gt;next &amp;&amp; node-&gt;next-&gt;val &lt; num)
        node = node-&gt;next;
      if (node-&gt;next &amp;&amp; node-&gt;next-&gt;val == num) {
        found = true;
        node-&gt;next = node-&gt;next-&gt;next;
      }
    }
    return found;
  }
private:
  struct Node {
    Node(int val = -1, 
         shared_ptr&lt;Node&gt; next = nullptr, 
         shared_ptr&lt;Node&gt; down = nullptr): 
          val(val), next(next), down(down) {}
    int val;
    shared_ptr&lt;Node&gt; next;
    shared_ptr&lt;Node&gt; down;
  };
  
  shared_ptr&lt;Node&gt; head_;
};

/**
 * Your Skiplist object will be instantiated and called as such:
 * Skiplist* obj = new Skiplist();
 * bool param_1 = obj-&gt;search(target);
 * obj-&gt;add(num);
 * bool param_3 = obj-&gt;erase(num);
 */</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">import random

class Node:
  def __init__(self, val=-1, right=None, down=None):
    self.val = val
    self.right = right
    self.down = down
    
class Skiplist:
  def __init__(self):
    self.head = Node() # Dummy head

  def search(self, target: int) -&gt; bool:
    node = self.head
    while node:
      # Move to the right in the current level
      while node.right and node.right.val &lt; target:
        node = node.right
      if node.right and node.right.val == target:
        return True
      # Move to the the next level
      node = node.down
    return False

  def add(self, num: int) -&gt; None:
    nodes = []
    node = self.head
    while node:
      # Move to the right in the current level
      while node.right and node.right.val &lt; num:
        node = node.right
      nodes.append(node)
      # Move to the next level
      node = node.down
    
    insert = True
    down = None
    while insert and nodes:
      node = nodes.pop()
      node.right = Node(num, node.right, down)
      down = node.right
      insert = (random.getrandbits(1) == 0)      
    
    # Create a new level with a dummy head.
    # right = None
    # down = current head
    if insert:
      self.head = Node(-1, None, self.head)

  def erase(self, num: int) -&gt; bool:
    node = self.head
    found = False
    while node:
      # Move to the right in the current level
      while node.right and node.right.val &lt; num:
        node = node.right
      # Find the target node
      if node.right and node.right.val == num:
        # Delete by skipping
        node.right = node.right.right
        found = True
      # Move to the next level
      node = node.down      
    return found
        
# Your Skiplist object will be instantiated and called as such:
# obj = Skiplist()
# param_1 = obj.search(target)
# obj.add(num)
# param_3 = obj.erase(num)</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/desgin/leetcode-1206-design-skiplist/">花花酱 LeetCode 1206. Design Skiplist</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/desgin/leetcode-1206-design-skiplist/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
