<?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>boundary Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/boundary/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/boundary/</link>
	<description></description>
	<lastBuildDate>Mon, 13 Aug 2018 20:29:45 +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>boundary Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/boundary/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 SP5 Binary Search</title>
		<link>https://zxi.mytechroad.com/blog/sp/sp5-binary-search/</link>
					<comments>https://zxi.mytechroad.com/blog/sp/sp5-binary-search/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 13 Aug 2018 06:41:04 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[SP]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[boundary]]></category>
		<category><![CDATA[sp]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3504</guid>

					<description><![CDATA[<p>Template: Time complexity: O(log(r-l)) * O(f(m) + g(m)) Space complexity: O(1) [crayon-66394217a7b3f228054949/] &#160; Slides: Lower Bound / Upper Bound [crayon-66394217a7b44145839474/] Mentioned Problems 花花酱 LeetCode. 69&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sp/sp5-binary-search/">花花酱 SP5 Binary Search</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><iframe width="500" height="375" src="https://www.youtube.com/embed/v57lNF2mb_s?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p>Template:</p>
<p>Time complexity: O(log(r-l)) * O(f(m) + g(m))</p>
<p>Space complexity: O(1)</p><pre class="crayon-plain-tag">"""
Returns the smallest number m such that g(m) is true.
"""
def binary_search(l, r):
  while l &lt; r:
    m = l + (r - l) // 2
    if f(m): return m    # if m is the answer
    if g(m):
      r = m              # new range [l, m)
    else
      l = m + 1          # new range [m+1, r)
  return l               # or not found</pre><p>&nbsp;</p>
<p>Slides:</p>
<p><img class="alignnone size-full wp-image-3510" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><img class="alignnone size-full wp-image-3509" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><img class="alignnone size-full wp-image-3508" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-3.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><img class="alignnone size-full wp-image-3507" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-4.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-4.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-4-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-4-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><img class="alignnone size-full wp-image-3506" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-5.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-5.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-5-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-5-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /><img class="alignnone size-full wp-image-3505" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-6.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-6.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-6-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/sp5-6-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p>Lower Bound / Upper Bound</p><pre class="crayon-plain-tag">#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;

int upper_bound(const vector&lt;int&gt;&amp; A, int val, int l, int r) {
  while (l &lt; r) {
    int m = l + (r - l) / 2;
    if (A[m] &gt; val)
      r = m;
    else      
      l = m + 1;
  }
  return l;
}

int lower_bound(const vector&lt;int&gt;&amp; A, int val, int l, int r) {
  while (l &lt; r) {
    int m = l + (r - l) / 2;
    if (A[m] &gt;= val)
      r = m;
    else
      l = m + 1;
  }
  return l;
}

int main() {
  vector&lt;int&gt; A{1, 2, 2, 2, 4, 4, 5};
  cout &lt;&lt; lower_bound(A, 0, 0, A.size()) &lt;&lt; endl; // 0
  cout &lt;&lt; lower_bound(A, 2, 0, A.size()) &lt;&lt; endl; // 1
  cout &lt;&lt; lower_bound(A, 3, 0, A.size()) &lt;&lt; endl; // 4
  cout &lt;&lt; upper_bound(A, 2, 0, A.size()) &lt;&lt; endl; // 4
  cout &lt;&lt; upper_bound(A, 4, 0, A.size()) &lt;&lt; endl; // 6
  cout &lt;&lt; upper_bound(A, 5, 0, A.size()) &lt;&lt; endl; // 7
}</pre><p></p>
<h1><strong>Mentioned Problems</strong></h1>
<ul>
<li><a href="https://zxi.mytechroad.com/blog/math/leetcode-69-sqrtx/">花花酱 LeetCode. 69 Sqrt(x)</a></li>
<li><a href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-704-binary-search/">花花酱 LeetCode 704. Binary Search</a></li>
<li><a href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-875-koko-eating-bananas/">花花酱 LeetCode 875. Koko Eating Bananas</a></li>
<li><a href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-378-kth-smallest-element-in-a-sorted-matrix/">花花酱 LeetCode 378. Kth Smallest Element in a Sorted Matrix</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sp/sp5-binary-search/">花花酱 SP5 Binary Search</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/sp/sp5-binary-search/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
