{"id":918,"date":"2020-04-02T21:38:53","date_gmt":"2020-04-03T04:38:53","guid":{"rendered":"http:\/\/209.126.2.187\/?p=918"},"modified":"2020-04-03T17:59:44","modified_gmt":"2020-04-04T00:59:44","slug":"binary-search-the-result","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2020\/04\/02\/binary-search-the-result\/","title":{"rendered":"Binary Search the Result"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Summary <\/h2>\n\n\n\n<p>In this post, I will introduce some problems which could binary search the best result.<\/p>\n\n\n\n<p>The problems in this category usually ask for a best value. The value could fall into the range of <code>[min, max]<\/code>. Another important property is that if the value at the middle is not achievable, we could eliminate a half the range. <\/p>\n\n\n\n<p>A possible solution is that we binary search this best value, and check if it is achievable or not. Repeat until we get a single answer.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">OJ<\/h2>\n\n\n\n\n<table id=\"tablepress-6\" class=\"tablepress tablepress-id-6\">\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\/maximum-average-subarray-ii\/\" rel=\"noopener noreferrer\" target=\"_blank\">LC 644 Maximum Average Subarray II<\/a><\/td><td class=\"column-2\">Binary Search Result + Greedy + Maximum Subarray Sum<\/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\/split-array-largest-sum\/submissions\/\" rel=\"noopener noreferrer\" target=\"_blank\">LC 410  Split Array Largest Sum<\/a><\/td><td class=\"column-2\">Binary Search Result + Greedy<\/td><td class=\"column-3\">6-7 points<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<!-- #tablepress-6 from cache -->\n\n\n\n<h2 class=\"wp-block-heading\">Details<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\"><a rel=\"noreferrer noopener\" href=\"https:\/\/leetcode.com\/problems\/split-array-largest-sum\/submissions\/\" target=\"_blank\">LC 410<\/a> Split Array Largest Sum<\/h4>\n\n\n\n<p>Given an array which consists of non-negative integers and an integer\u00a0<em>m<\/em>, you can split the array into\u00a0<em>m<\/em>\u00a0non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these\u00a0<em>m<\/em>\u00a0subarrays.<\/p>\n\n\n\n<p>The DP solution is not difficult.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">\/\/ alg[n][M] = 0\nalg[i][m] = \n   min{max(alg[j + 1][m - 1], sum[i, ..., j]), for j in [i, ..., n - 1]}<\/code><\/pre>\n\n\n\n<p>The problem could be solved via binary searching the result. <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The minimum value possible is <code>min_element(nums)<\/code>; the maximum value possible is <code>sum(nums)<\/code><\/li><li>If a value <code>mid<\/code> is not possible, it is not possible for values smaller than mid;  <\/li><li><code>check<\/code> could be done in <code>O(n)<\/code> by greedily dividing groups: from left to right, as long as current sum is smaller than <code>mid<\/code>, insert it into the current group. If finally <code>count &lt;= K<\/code>, we know <code>mid<\/code> is possible, since we could further divide some groups but not increase the largest sum. The tricky part is if <code>count > K<\/code>, we know all smaller values are not possible. This could be proved by contradiction. If a smaller value is possible, the first subarray would be smaller, so is the second array, which causes the number of subarrays will be not smaller than <code>count<\/code>, which causes the contradiction. <\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will introduce some problems which could binary search the best result. The problems in this category usually ask for a best value. The value could fall into the range of [min, max]. Another important property is that if the value at the middle is not achievable, we could eliminate a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,57],"tags":[],"class_list":["post-918","post","type-post","status-publish","format-standard","hentry","category-alg","category-binary-search"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/918","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=918"}],"version-history":[{"count":7,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/918\/revisions"}],"predecessor-version":[{"id":928,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/918\/revisions\/928"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}