{"id":3504,"date":"2018-08-12T23:41:04","date_gmt":"2018-08-13T06:41:04","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3504"},"modified":"2018-08-13T13:29:45","modified_gmt":"2018-08-13T20:29:45","slug":"sp5-binary-search","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/sp5-binary-search\/","title":{"rendered":"\u82b1\u82b1\u9171 SP5 Binary Search"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode Binary Search - \u5237\u9898\u627e\u5de5\u4f5c SP5\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/v57lNF2mb_s?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>Template:<\/p>\n<p>Time complexity: O(log(r-l)) * O(f(m) + g(m))<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:default decode:true\">\"\"\"\r\nReturns the smallest number m such that g(m) is true.\r\n\"\"\"\r\ndef binary_search(l, r):\r\n  while l &lt; r:\r\n    m = l + (r - l) \/\/ 2\r\n    if f(m): return m    # if m is the answer\r\n    if g(m):\r\n      r = m              # new range [l, m)\r\n    else\r\n      l = m + 1          # new range [m+1, r)\r\n  return l               # or not found\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Slides:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p>Lower Bound \/ Upper Bound<\/p>\n<pre class=\"lang:c++ decode:true \">#include &lt;iostream&gt;\r\n#include &lt;vector&gt;\r\nusing namespace std;\r\n\r\nint upper_bound(const vector&lt;int&gt;&amp; A, int val, int l, int r) {\r\n  while (l &lt; r) {\r\n    int m = l + (r - l) \/ 2;\r\n    if (A[m] &gt; val)\r\n      r = m;\r\n    else      \r\n      l = m + 1;\r\n  }\r\n  return l;\r\n}\r\n\r\nint lower_bound(const vector&lt;int&gt;&amp; A, int val, int l, int r) {\r\n  while (l &lt; r) {\r\n    int m = l + (r - l) \/ 2;\r\n    if (A[m] &gt;= val)\r\n      r = m;\r\n    else\r\n      l = m + 1;\r\n  }\r\n  return l;\r\n}\r\n\r\nint main() {\r\n  vector&lt;int&gt; A{1, 2, 2, 2, 4, 4, 5};\r\n  cout &lt;&lt; lower_bound(A, 0, 0, A.size()) &lt;&lt; endl; \/\/ 0\r\n  cout &lt;&lt; lower_bound(A, 2, 0, A.size()) &lt;&lt; endl; \/\/ 1\r\n  cout &lt;&lt; lower_bound(A, 3, 0, A.size()) &lt;&lt; endl; \/\/ 4\r\n  cout &lt;&lt; upper_bound(A, 2, 0, A.size()) &lt;&lt; endl; \/\/ 4\r\n  cout &lt;&lt; upper_bound(A, 4, 0, A.size()) &lt;&lt; endl; \/\/ 6\r\n  cout &lt;&lt; upper_bound(A, 5, 0, A.size()) &lt;&lt; endl; \/\/ 7\r\n}<\/pre>\n<h1><strong>Mentioned Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-69-sqrtx\/\">\u82b1\u82b1\u9171 LeetCode. 69 Sqrt(x)<\/a><\/li>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-704-binary-search\/\">\u82b1\u82b1\u9171 LeetCode 704. Binary Search<\/a><\/li>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-875-koko-eating-bananas\/\">\u82b1\u82b1\u9171 LeetCode 875. Koko Eating Bananas<\/a><\/li>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-378-kth-smallest-element-in-a-sorted-matrix\/\">\u82b1\u82b1\u9171 LeetCode 378. Kth Smallest Element in a Sorted Matrix<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Template: Time complexity: O(log(r-l)) * O(f(m) + g(m)) Space complexity: O(1) &#8220;&#8221;&#8221; Returns the smallest number m such that g(m) is true. &#8220;&#8221;&#8221; def binary_search(l,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149,170],"tags":[52,373,374],"class_list":["post-3504","post","type-post","status-publish","format-standard","hentry","category-binary-search","category-sp","tag-binary-search","tag-boundary","tag-sp","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3504","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=3504"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3504\/revisions"}],"predecessor-version":[{"id":3516,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3504\/revisions\/3516"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}