{"id":7125,"date":"2020-07-19T01:02:01","date_gmt":"2020-07-19T08:02:01","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7125"},"modified":"2020-07-19T01:02:46","modified_gmt":"2020-07-19T08:02:46","slug":"leetcode-1521-find-a-value-of-a-mysterious-function-closest-to-target","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-1521-find-a-value-of-a-mysterious-function-closest-to-target\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1521. Find a Value of a Mysterious Function Closest to Target"},"content":{"rendered":"\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/07\/09\/change.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Winston was given the above mysterious function&nbsp;<code>func<\/code>. He has an integer array&nbsp;<code>arr<\/code>&nbsp;and an integer&nbsp;<code>target<\/code>&nbsp;and he wants to find the values&nbsp;<code>l<\/code>&nbsp;and&nbsp;<code>r<\/code>&nbsp;that make&nbsp;the value&nbsp;<code>|func(arr, l, r) - target|<\/code>&nbsp;minimum possible.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum possible value<\/em>&nbsp;of&nbsp;<code>|func(arr, l, r) - target|<\/code>.<\/p>\n\n\n\n<p>Notice that&nbsp;<code>func<\/code>&nbsp;should be called with the values&nbsp;<code>l<\/code>&nbsp;and&nbsp;<code>r<\/code>&nbsp;where&nbsp;<code>0 &lt;= l, r &lt; arr.length<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [9,12,3,7,15], target = 5\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> Calling func with all the pairs of [l,r] = [[0,0],[1,1],[2,2],[3,3],[4,4],[0,1],[1,2],[2,3],[3,4],[0,2],[1,3],[2,4],[0,3],[1,4],[0,4]], Winston got the following results [9,12,3,7,15,8,0,3,7,0,0,3,0,0,0]. The value closest to 5 is 7 and 3, thus the minimum difference is 2.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1000000,1000000,1000000], target = 1\n<strong>Output:<\/strong> 999999\n<strong>Explanation:<\/strong> Winston called the func with all possible values of [l,r] and he always got 1000000, thus the min difference is 999999.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1,2,4,8,16], target = 0\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length &lt;= 10^5<\/code><\/li><li><code>1 &lt;= arr[i] &lt;= 10^6<\/code><\/li><li><code>0 &lt;= target &lt;= 10^7<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force w\/ Optimization<\/strong><\/h2>\n\n\n\n<p>Try all possible [l, r] range with pruning. <br>1. for a given l, we extend r, since s &amp; x &lt;= s, if s becomes less than target, we can stop the inner loop.<br>2. Case 1, s = arr[l] &amp; &#8230; &amp; arr[n-1], s &gt; target, <br>    Let s&#8217; = arr[l+1] &amp; &#8230; &amp; arr[n-1], s&#8217; &gt;= s,<br>    if s &gt; target, then s&#8217; &gt; target, we can stop outer loop as well.<br>   Case 2, inner loop stops at r, s = arr[l] &amp; &#8230; &amp; arr[r], s &lt;= target, we continue with l+1.<\/p>\n\n\n\n<p>Time complexity: O(n)? on average, O(n^2) in worst case.<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int closestToTarget(vector<int>& arr, int target) {\n    const int n = arr.size();\n    int ans = INT_MAX;\n    for (int i = 0; i < n; ++i) {\n      int s = arr[i];\n      for (int j = i; j < n; ++j) {\n        s &#038;= arr[j];\n        ans = min(ans, abs(s - target));\n        if (ans == 0) return 0;\n        if (s <= target) break; \/\/ s is decreasing.\n      }\n      if (s > target) break; \/\/ s is increasing.\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Winston was given the above mysterious function&nbsp;func. He has an integer array&nbsp;arr&nbsp;and an integer&nbsp;target&nbsp;and he wants to find the values&nbsp;l&nbsp;and&nbsp;r&nbsp;that make&nbsp;the value&nbsp;|func(arr, l, r) -&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[52,16,217,215],"class_list":["post-7125","post","type-post","status-publish","format-standard","hentry","category-bit","tag-binary-search","tag-bit","tag-hard","tag-sliding-window","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7125","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=7125"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7125\/revisions"}],"predecessor-version":[{"id":7127,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7125\/revisions\/7127"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}