{"id":4067,"date":"2018-09-23T23:02:15","date_gmt":"2018-09-24T06:02:15","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4067"},"modified":"2018-09-25T08:08:58","modified_gmt":"2018-09-25T15:08:58","slug":"leetcode-908-smallest-range-i","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-908-smallest-range-i\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 908. Smallest Range I"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given an array\u00a0<code>A<\/code>\u00a0of integers, for each integer\u00a0<code>A[i]<\/code>\u00a0we may choose any\u00a0<code>x<\/code>\u00a0with\u00a0<code>-K &lt;= x &lt;= K<\/code>, and add\u00a0<code>x<\/code>to\u00a0<code>A[i]<\/code>.<\/p>\n<p>After this process, we have some array\u00a0<code>B<\/code>.<\/p>\n<p>Return the smallest possible difference between the maximum value of\u00a0<code>B<\/code>\u00a0and the minimum value of\u00a0<code>B<\/code>.<\/p>\n<div>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>A = <span id=\"example-input-1-1\">[1]<\/span>, K = <span id=\"example-input-1-2\">0<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">0\r\n<strong>Explanation<\/strong>: B = [1]<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>A = <span id=\"example-input-2-1\">[0,10]<\/span>, K = <span id=\"example-input-2-2\">2<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">6\r\n<\/span><span id=\"example-output-1\"><strong>Explanation<\/strong>: B = [2,8]<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong>A = <span id=\"example-input-3-1\">[1,3,6]<\/span>, K = <span id=\"example-input-3-2\">3<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">0\r\n<\/span><span id=\"example-output-1\"><strong>Explanation<\/strong>: B = [3,3,3] or B = [4,4,4]<\/span>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= A.length &lt;= 10000<\/code><\/li>\n<li><code>0 &lt;= A[i] &lt;= 10000<\/code><\/li>\n<li><code>0 &lt;= K &lt;= 10000<\/code><\/li>\n<\/ol>\n<h1>Solution 0: Brute Force (TLE)<\/h1>\n<p>Try all pairs<\/p>\n<p>Time complexity: O(n^2)<\/p>\n<p>Space complexity: O(1)<\/p>\n<h1>Solution 1: Math<\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>Find the min\/max element of the array.<\/p>\n<p>min + k v.s. max &#8211; k<\/p>\n<p>ans = max(0, (max &#8211; min) &#8211; 2 * k))<\/p>\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\nclass Solution {\r\npublic:\r\n  int smallestRangeI(vector&lt;int&gt;&amp; A, int K) {\r\n    int a_min = *min_element(begin(A), end(A));\r\n    int a_max = *max_element(begin(A), end(A));\r\n    return max(0, (a_max - a_min) - 2 * K);\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true\"># Author: Huahua, 72 ms\r\nclass Solution:\r\n  def smallestRangeI(self, A, K):\r\n    return max(0, max(A) - min(A) - 2 * K);<\/pre>\n<\/div><\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an array\u00a0A\u00a0of integers, for each integer\u00a0A[i]\u00a0we may choose any\u00a0x\u00a0with\u00a0-K &lt;= x &lt;= K, and add\u00a0xto\u00a0A[i]. After this process, we have some array\u00a0B. Return&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[413,31,139],"class_list":["post-4067","post","type-post","status-publish","format-standard","hentry","category-math","tag-all-pairs","tag-math","tag-range","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4067","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=4067"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4067\/revisions"}],"predecessor-version":[{"id":4072,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4067\/revisions\/4072"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}