题目 :

Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and add x to A[i].

After this process, we have some array B.

Return the smallest possible difference between the maximum value of B and the minimum value of B.

 Example 1:

Input: A = [1], K = 0
Output: 0
Explanation: B = [1]
Example 2: Input: A = [0,10], K = 2
Output: 6
Explanation: B = [2,8]
Example 3: Input: A = [1,3,6], K = 3
Output: 0
Explanation: B = [3,3,3] or B = [4,4,4]

解题思路 :

因为题目希望 从所有数组B中,计算出所有最大值和最小值的差值,并从中取最小的差值

所以我们的目标是使数组B中的 最大值尽可能小,最小值尽可能大

第一步确定最大值里,最小的,因为max元素最多只能减小到 max - K, 所以最大值不可能小于 max - K

第二步确定最小值里,最大的,因为min元素最多只能增加到 min + K, 所以最小值不可能大于 min + K

比较max - K 和 min + K

如果max - K <= min + K, 其他元素的取值范围正好可以覆盖[max - K, min + K]的区间, 因为每一个元素a[i], a[i] + K >= min + K, a[i] - K <= max - K

说明所有元素可以取到相同值,则最大值和最小值差距可以为0

如果max - K > min + K, 则由于最小的最大值为max - K, 最大的最小值为min + K,

则差距为 max - min - 2 * K

c++代码

class Solution {
public:
//how to prove this process
//min min + k
//max max - k
//如果min + k >= max - k 其它元素的取值范围一定是可以覆盖min + k, max - k
//所以是最小值是0
//如果min + k < max - k 则最大和最小的差值 = max - min - 2 * k
int smallestRangeI(vector<int>& A, int K) {
//edge case
int minV = INT_MAX;
int maxV = INT_MIN;
for (int d : A) {
minV = min(d, minV);
maxV = max(d, maxV);
} int ans = 0;
if (maxV - K <= minV + K) return ans; return maxV - minV - 2 * K;
}
};

java代码

class Solution {
public int smallestRangeI(int[] A, int K) {
int minV = Integer.MAX_VALUE;
int maxV = Integer.MIN_VALUE;
int ans = 0;
for (int d : A) {
minV = Math.min(minV, d);
maxV = Math.max(maxV, d);
} if (minV + K >= maxV - K) return ans;
return maxV - minV - 2 * K;
}
}

解题报告-908. Smallest Range I的更多相关文章

  1. 【Leetcode_easy】908. Smallest Range I

    problem 908. Smallest Range I solution: class Solution { public: int smallestRangeI(vector<int> ...

  2. [LeetCode] 908. Smallest Range I 最小区间

    Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...

  3. 【LeetCode】908. Smallest Range I 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...

  4. LeetCode 908 Smallest Range I 解题报告

    题目要求 Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K ...

  5. [LeetCode&Python] Problem 908. Smallest Range I

    Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...

  6. 【leetcode】908. Smallest Range I

    题目如下: 解题思路:简单的不能再简单的题目了,对于任意一个A[i]来说,其可能的最小的最大值是A[i]-K,最大的最小值是A[i]+K.遍历数组,求出所有元素中最大的最小值和最小的最大值,两者之差( ...

  7. 908. Smallest Range I

    Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...

  8. 【LeetCode】632. Smallest Range 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/smallest ...

  9. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

随机推荐

  1. (转)List<T>的各种排序方法

    近日,在工作的时候遇到要对一个大的List<T>集合进行排序,于是就了解下各种List<T>的排序方法. 首先,排序自然就会想到用Sort方法,看看List<T>的 ...

  2. 线上服务器TCP被打满是啥情况

    从一个线上服务器警告谈谈backlog https://wangxiangnan.cc/?p=105 缘起 双十一如期而至,此时的我因为在处理客户的一个问题已经陷入了忙碌.突然,不断接到驻场实施发来的 ...

  3. UT源码+105032014070

    设计三角形问题的程序 输入三个整数a.b.c,分别作为三角形的三条边,现通过程序判断由三条边构成的三角形的类型为等边三角形.等腰三角形.一般三角形(特殊的还有直角三角形),以及不构成三角形.(等腰直角 ...

  4. Mac下忘记mysql的root密码

    cd /usr/local/mysql/bin sudo su sudo /usr/local/mysql/support-files/mysql.server stop # ./mysqld_saf ...

  5. c++ 声明和定义的区别

    从编译原理上来说,声明是仅仅告诉编译器,有个某类型的变量会被使用,但是编译器并不会为它分配任何内存.而定义就是分配了内存. int a;在外面是作为一个语句,这就是定义,会构造对象,定义本身也是声明. ...

  6. DCI改进,发布后作业乱码不能打开

    1.发布后作业不能打开问题,找到com.comsys.net.cn.dci.ui.dialog.PublishesDialog 的960行,改为这样: //以前没有指定文件编码前,采用系统默认编码 / ...

  7. Centos 6 安装 配置 oracle11g R2

    1.安装centos6.3_64位: 下载地址:http://mirror.bit.edu.cn/centos/6.3/isos/x86_64/ CentOS-6.3-x86_64-bin-DVD1. ...

  8. 希尔排序算法-python实现

    #-*- coding: UTF-8 -*- import numpy as np def ShellSort(a): gap = a.size / 2 while gap >= 1: for ...

  9. .NET 4.5 HttpClient 中使用Cookie

    为了使用.NET 4.5的HttpClient从WIN2K3换成了WIN7.装VS2010,结果告诉我VS2010不支持.NET 4.5.又装VS2012,接着装.NET FRAMEWORK 4.5. ...

  10. 【free() invalid next size】谨慎地在C++的类中存储指针来方便访问其他节点

    “我跟你们说,你们知道STL容器,vector/string/deque等等,都有个reserve方法吗?你们一个个地push_back,嫌C++比C慢,怪谁?” “要像我这样,预先分配足够大的空间, ...