{"id":871,"date":"2020-03-29T12:02:57","date_gmt":"2020-03-29T19:02:57","guid":{"rendered":"http:\/\/209.126.2.187\/?p=871"},"modified":"2020-03-29T12:03:27","modified_gmt":"2020-03-29T19:03:27","slug":"merge-sort","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2020\/03\/29\/merge-sort\/","title":{"rendered":"Merge Sort"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In this post, I will introduce some problems using the merge sort technique. <\/p>\n\n\n\n<p>These problems often ask, for an element at index <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-2a39988ef137861995035b4ea74997c3_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#105;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"6\" style=\"vertical-align: 0px;\"\/>, how many <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-f5c5a26211692edf4daa541f1b87759f_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#106;\" title=\"Rendered by QuickLaTeX.com\" height=\"16\" width=\"9\" style=\"vertical-align: -4px;\"\/> are there which satisfies a formula. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">OJ<\/h2>\n\n\n\n\n<table id=\"tablepress-3\" class=\"tablepress tablepress-id-3\">\n<thead>\n<tr class=\"row-1\">\n\t<td class=\"column-1\"><\/td><th class=\"column-2\">Trick<\/th><th class=\"column-3\">Difficulty<\/th>\n<\/tr>\n<\/thead>\n<tbody class=\"row-striping row-hover\">\n<tr class=\"row-2\">\n\t<td class=\"column-1\"><a href=\"https:\/\/leetcode.com\/problems\/count-number-of-teams\/\" rel=\"noopener noreferrer\" target=\"_blank\">LC 1395 <\/a><\/td><td class=\"column-2\">Enumerate the middle element + Count #elements larger or smaller<br \/>\n<\/td><td class=\"column-3\">6 points<\/td>\n<\/tr>\n<tr class=\"row-3\">\n\t<td class=\"column-1\"><a href=\"https:\/\/leetcode.com\/problems\/count-of-range-sum\/\" rel=\"noopener noreferrer\" target=\"_blank\">LC 327 <\/a><\/td><td class=\"column-2\">Prefix sum + Keep diff in range<\/td><td class=\"column-3\">6-7 points<\/td>\n<\/tr>\n<tr class=\"row-4\">\n\t<td class=\"column-1\"><a href=\"https:\/\/leetcode.com\/problems\/reverse-pairs\/\" rel=\"noopener noreferrer\" target=\"_blank\">LC 493 <\/a><\/td><td class=\"column-2\">Count #elements larger or smaller<\/td><td class=\"column-3\">6 points<\/td>\n<\/tr>\n<tr class=\"row-5\">\n\t<td class=\"column-1\"><a href=\"https:\/\/leetcode.com\/problems\/count-of-smaller-numbers-after-self\/\" rel=\"noopener noreferrer\" target=\"_blank\">LC 315 <\/a><\/td><td class=\"column-2\">Count #elements larger or smaller<\/td><td class=\"column-3\">6 points<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<!-- #tablepress-3 from cache -->\n\n\n\n<h2 class=\"wp-block-heading\">Details<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">The Master Theorem<\/h4>\n\n\n\n<p> <\/p>\n\n\n\n<p><p class=\"ql-center-displayed-equation\" style=\"line-height: 32px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-578b8ac7f42de75ba9777a0056462887_l3.png\" height=\"32\" width=\"203\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#091;&#84;&#40;&#110;&#41;&#32;&#61;&#32;&#97;&#92;&#116;&#105;&#109;&#101;&#115;&#40;&#84;&#40;&#92;&#102;&#114;&#97;&#99;&#123;&#110;&#125;&#123;&#98;&#125;&#41;&#41;&#32;&#43;&#32;&#102;&#40;&#110;&#41;&#92;&#093;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p><\/p>\n\n\n\n<p>If <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-d7eb7095bca5185c5bbb040ab372c7dc_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#102;&#40;&#110;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"35\" style=\"vertical-align: -4px;\"\/> is of complexity <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-ef9c4f463a6c75a3c8f44a92fca86d61_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#79;&#40;&#110;&#94;&#123;&#108;&#111;&#103;&#95;&#98;&#94;&#97;&#125;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"64\" style=\"vertical-align: -4px;\"\/>, then <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-f99ce20535751f7d580ad9dfcd931d14_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#84;&#40;&#110;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"37\" style=\"vertical-align: -4px;\"\/> is of complexity <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-a4248af063418872f69ee1fcebc866b3_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#79;&#40;&#110;&#94;&#123;&#108;&#111;&#103;&#95;&#98;&#94;&#97;&#125;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#108;&#111;&#103;&#110;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"120\" style=\"vertical-align: -4px;\"\/><\/p>\n\n\n\n<p>So in these problems, a key point is to keep the merge part of complexity <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-0754944448474b6582ffaff732912a29_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#79;&#40;&#110;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"38\" style=\"vertical-align: -4px;\"\/> by leveraging the sorted property. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Keep Difference in Range<\/h4>\n\n\n\n<p>I want to talk about the merge part of <a href=\"https:\/\/leetcode.com\/problems\/count-of-range-sum\/\">LC 327<\/a> in this section. Given two sorted array <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-1e8f748fe72ec4b5bc48d62b8b1e66f2_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#110;&#117;&#109;&#115;&#091;&#108;&#101;&#102;&#116;&#58;&#109;&#105;&#100;&#093;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"129\" style=\"vertical-align: -5px;\"\/> and <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-8685858da3cae9984e62b0be84c81f1a_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#110;&#117;&#109;&#115;&#091;&#109;&#105;&#100;&#43;&#49;&#58;&#114;&#105;&#103;&#104;&#116;&#093;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"168\" style=\"vertical-align: -5px;\"\/>, the problem is to count how many <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-51fa29c1331f0be79faa3272db2c083f_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#123;&#105;&#44;&#92;&#32;&#106;&#92;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"44\" style=\"vertical-align: -5px;\"\/> are there, such that<\/p>\n\n\n\n<p><p class=\"ql-center-displayed-equation\" style=\"line-height: 18px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-861553ad985ce0b2a83cd02c1204343a_l3.png\" height=\"18\" width=\"605\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#091;&#108;&#111;&#119;&#101;&#114;&#32;&#60;&#61;&#32;&#110;&#117;&#109;&#115;&#091;&#106;&#093;&#32;&#45;&#32;&#110;&#117;&#109;&#115;&#091;&#105;&#093;&#32;&#60;&#61;&#32;&#117;&#112;&#112;&#101;&#114;&#44;&#32;&#105;&#32;&#92;&#105;&#110;&#32;&#091;&#108;&#101;&#102;&#116;&#58;&#109;&#105;&#100;&#093;&#92;&#32;&#97;&#110;&#100;&#92;&#32;&#106;&#32;&#92;&#105;&#110;&#32;&#091;&#109;&#105;&#100;&#43;&#49;&#58;&#114;&#105;&#103;&#104;&#116;&#093;&#92;&#093;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p><\/p>\n\n\n\n<p>Usually, we will enumerate the left part (or the right part) by <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-2a39988ef137861995035b4ea74997c3_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#105;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"6\" style=\"vertical-align: 0px;\"\/>. We use two cursors <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-769fbae46ae5f1891683452124aa2ed6_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#115;&#116;&#97;&#114;&#116;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"39\" style=\"vertical-align: 0px;\"\/> and <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nanzhou.cc\/wp-content\/ql-cache\/quicklatex.com-e4fec6140abdb8aa708d61c1c0fee7d0_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#101;&#110;&#100;\" title=\"Rendered by QuickLaTeX.com\" height=\"13\" width=\"29\" style=\"vertical-align: 0px;\"\/> to store the boundary in the right part to satisfy the above formula.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int mid = left + (right - left) \/ 2;\nint result = merge_sort(left, mid, nums) + merge_sort(mid + 1, right, nums);\nint start = mid + 1, end = mid + 1;\nfor (int i = left; i &lt; mid + 1; ++i) {\n    while (start &lt; right + 1 &amp;&amp; (long)nums[start] - nums[i] &lt; lower) {\n        start++;\n    }\n    while (end &lt; right + 1 &amp;&amp; (long)nums[end] - nums[i] &lt;= upper) {\n        end++;\n    }\n    result += (end - start);\n}\ninplace_merge(nums.begin() + left, nums.begin() + mid + 1, nums.begin() + right + 1);\nreturn result;<\/code><\/pre>\n\n\n\n<p> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will introduce some problems using the merge sort technique. These problems often ask, for an element at index , how many are there which satisfies a formula. OJ Details The Master Theorem &nbsp; &nbsp; If is of complexity , then is of complexity So in these problems, a key point&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[],"class_list":["post-871","post","type-post","status-publish","format-standard","hentry","category-sort"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=871"}],"version-history":[{"count":10,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/871\/revisions"}],"predecessor-version":[{"id":885,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/871\/revisions\/885"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}