题目 :

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. Oracle(一)安装

    一.到官网或者哪里去下载Oracle,我下的是winX64的11g版本 官网:https://www.oracle.com/technetwork/database/enterprise-editio ...

  2. BZOJ4886: [Lydsy1705月赛]叠塔游戏(环套树森林&贪心)

    4886: [Lydsy1705月赛]叠塔游戏 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 198  Solved: 76[Submit][Stat ...

  3. java - 百钱百鸡小算法

    传送门: 袁咩咩的小小博客 百钱百鸡是一个非常经典的不定方程问题,最早源于我国古代的<算经>,这是古代著名数学家张丘建首次提出的.百钱百鸡问题原文如下: 鸡翁一,值钱五,鸡母一,值钱三,鸡 ...

  4. 51Nod 1088:最长回文子串(暴力)

    1088 最长回文子串  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串. 输入 ...

  5. CH1808 Milking Grid

    题意 POJ2185 数据加强版 描述 Every morning when they are milked, the Farmer John's cows form a rectangular gr ...

  6. IDEA java开发学习笔记

    JDK8下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 配置环境变量 ...

  7. 智能家居入门DIY——【一、ESP8266之软串口HTTP请求】

    前段时间做了一个激光雕刻,玩的不亦乐乎.对Arduino大感兴趣,于是又入手一块20大洋版,配上买学习套件时的诸多零件——红外发射管.一体化红外接收头.DHT11温湿度传感器.ESP8266等,以及某 ...

  8. CSS布局模型学习

    转自:http://www.cnblogs.com/erliang/p/4092192.html CSS布局模型学习   参考链接慕课网:HTML+CSS基础课程 知识基础 1. 样式 内联 嵌入 外 ...

  9. android中的一个圆角图片

    RoundedImageView A fast ImageView (and Drawable) that supports rounded corners (and ovals or circles ...

  10. java高并发总结-常用于面试复习

    定义: 独占锁是一种悲观保守的加锁策略,它避免了读/读冲突,如果某个只读线程获取锁,则其他读线程都只能等待,这种情况下就限制了不必要的并发性,因为读操作并不会影响数据的一致性. 共享锁则是一种乐观锁, ...