{"id":747,"date":"2017-11-08T08:33:09","date_gmt":"2017-11-08T16:33:09","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=747"},"modified":"2018-08-31T12:30:02","modified_gmt":"2018-08-31T19:30:02","slug":"leetcode-4-median-of-two-sorted-arrays","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-4-median-of-two-sorted-arrays\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode  4. Median of Two Sorted Arrays"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 4. Median of Two Sorted Arrays - \u5237\u9898\u627e\u5de5\u4f5c EP102\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/KB9IcSCDQ9k?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>\u9898\u76ee\u5927\u610f\uff1a\u6c42\u4e24\u4e2a\u5df2\u7ecf\u6392\u5e8f\u7684\u6570\u7ec4\u7684\u4e2d\u4f4d\u6570\uff08\u5982\u679c\u5408\u5e76\u540e\uff09\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>There are two sorted arrays\u00a0<b>nums1<\/b>\u00a0and\u00a0<b>nums2<\/b>\u00a0of size m and n respectively.<\/p>\n<p>Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">nums1 = [1, 3]\r\nnums2 = [2]\r\n\r\nThe median is 2.0\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">nums1 = [1, 2]\r\nnums2 = [3, 4]\r\n\r\nThe median is (2 + 3)\/2 = 2.5<\/pre>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Binary Search<\/p>\n<p>Time complexity: O(log(min(n1,n2)))<\/p>\n<p>Space complexity: O(1)<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/4-ep102.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-755\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/4-ep102.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/4-ep102.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/4-ep102-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/4-ep102-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/4-ep102-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/4-ep102-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-754\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/4-ep102-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/4-ep102-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/4-ep102-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/4-ep102-2-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/4-ep102-2-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\">\u00a0<\/ins><\/p>\n<h1><strong>Solution: Binary Search<\/strong><\/h1>\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\r\n\/\/ Runtime: 52 ms\r\nclass Solution {\r\npublic:\r\n    double findMedianSortedArrays(vector&lt;int&gt;&amp; nums1, vector&lt;int&gt;&amp; nums2) {\r\n        const int n1 = nums1.size();\r\n        const int n2 = nums2.size();\r\n        \/\/ Make sure n1 &lt;= n2\r\n        if (n1 &gt; n2) return findMedianSortedArrays(nums2, nums1);\r\n        \r\n        const int k = (n1 + n2 + 1) \/ 2;\r\n\r\n        int l = 0;\r\n        int r = n1;\r\n               \r\n        while (l &lt; r) {\r\n            const int m1 = l + (r - l) \/ 2;\r\n            const int m2 = k - m1;\r\n            if (nums1[m1] &lt; nums2[m2 - 1])\r\n                l = m1 + 1;\r\n            else\r\n                r = m1;\r\n        }\r\n        \r\n        const int m1 = l;\r\n        const int m2 = k - l;\r\n        \r\n        const int c1 = max(m1 &lt;= 0 ? INT_MIN : nums1[m1 - 1], \r\n                           m2 &lt;= 0 ? INT_MIN : nums2[m2 - 1]);\r\n\r\n        if ((n1 + n2) % 2 == 1)\r\n            return c1;    \r\n        \r\n        const int c2 = min(m1 &gt;= n1 ? INT_MAX : nums1[m1], \r\n                           m2 &gt;= n2 ? INT_MAX : nums2[m2]);\r\n                \r\n        return (c1 + c2) * 0.5;\r\n    }\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\r\n\/\/ Runtime: 66 ms\r\nclass Solution {\r\n    public double findMedianSortedArrays(int[] nums1, int[] nums2) {\r\n        int n1 = nums1.length;\r\n        int n2 = nums2.length;\r\n        if (n1 &gt; n2) \r\n            return findMedianSortedArrays(nums2, nums1);\r\n        \r\n        int k = (n1 + n2 + 1) \/ 2;\r\n        int l = 0;\r\n        int r = n1;\r\n               \r\n        while (l &lt; r) {\r\n            int m1 = l + (r - l) \/ 2;\r\n            int m2 = k - m1;\r\n            if (nums1[m1] &lt; nums2[m2 - 1])\r\n                l = m1 + 1;\r\n            else\r\n                r = m1;\r\n        }\r\n        \r\n        int m1 = l;\r\n        int m2 = k - l;\r\n        \r\n        int c1 = Math.max(m1 &lt;= 0 ? Integer.MIN_VALUE : nums1[m1 - 1], \r\n                          m2 &lt;= 0 ? Integer.MIN_VALUE : nums2[m2 - 1]);\r\n\r\n        if ((n1 + n2) % 2 == 1)\r\n            return c1;    \r\n        \r\n        int c2 = Math.min(m1 &gt;= n1 ? Integer.MAX_VALUE : nums1[m1], \r\n                          m2 &gt;= n2 ? Integer.MAX_VALUE : nums2[m2]);\r\n                \r\n        return (c1 + c2) * 0.5;\r\n    }\r\n}<\/pre>\n<\/div><\/div>\n<h1><strong>Related Problem:<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/leetcode\/leetcode-295-find-median-from-data-stream\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 295. Find Median from Data Stream O(logn) + O(1)<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u6c42\u4e24\u4e2a\u5df2\u7ecf\u6392\u5e8f\u7684\u6570\u7ec4\u7684\u4e2d\u4f4d\u6570\uff08\u5982\u679c\u5408\u5e76\u540e\uff09\u3002 Problem: There are two sorted arrays\u00a0nums1\u00a0and\u00a0nums2\u00a0of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity&#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,165],"tags":[52,217,11],"class_list":["post-747","post","type-post","status-publish","format-standard","hentry","category-binary-search","category-hard","tag-binary-search","tag-hard","tag-median","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/747","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=747"}],"version-history":[{"count":11,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/747\/revisions"}],"predecessor-version":[{"id":3789,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/747\/revisions\/3789"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}