题目 :

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. HDU 2807

    http://acm.hdu.edu.cn/showproblem.php?pid=2807 把矩阵相乘放在第二重循环,第三重循环只进行比较可以水过,优化的方法不懂 主要用这题练习floyd的写法 # ...

  2. CentOS6下yum升级安装mysql

    CentOS6默认版本的mysql是5.1.73,当前主流版本一般为mysql-5.6,需要安装该版本的话可以执行以下操作 1.1.卸载旧版mysql 1)备份数据 # 直接删除老版本的相关文件可能会 ...

  3. Python windows ping

    # -*- coding: utf-8 -*- import os # 参考文档: # Ping to a specific IP address using python [duplicate] # ...

  4. 【C#】App_LocalResources实现多语言

    介绍 如果您创建的网页将由使用不同语言的用户阅读,则必须为这些读者提供用他们自己的语言查看网页的方法.一种方法是分别用各语言重新创建页面,但这种方法可能需要大量工作量.容易出错并且在更改原始页时很难维 ...

  5. 设置Nginx日志

    在nginx.conf文件或vhosts/*.conf文件中的access_log日志中指定级别为main. http { include mime.types; default_type appli ...

  6. memsql 多节点部署

    以前部署使用的是docker,这个测试使用的是阿里云的机器 没有使用企业版,使用的是开发版,为一个master 多个Leaf 机器列表 172.31.128.165 172.31.128.166 17 ...

  7. linux下nginx安装、配置实战

    1什么是Nginx Nginx("enginex")是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器,在高连接并发的情况下Nginx是Apac ...

  8. Django models中关于blank与null

    建立一个简易Model class Person(models.Model): GENDER_CHOICES=( (1,'Male'), (2,'Female'), ) name=models.Cha ...

  9. 国产FPGA市场分析 该如何破局

    2018年上半年对于中国半导体行业而言是多事之秋,发生了几件让国人深入思考的大事.我作为IC产业的逃兵,最近也在思考很多的问题,包括资本市场.集成电路行业和研究所的一些不成熟的想法. 2008年进入华 ...

  10. Envoy 源码分析--程序启动过程

    目录 Envoy 源码分析--程序启动过程 初始化 main 入口 MainCommon 初始化 服务 InstanceImpl 初始化 启动 main 启动入口 服务启动流程 LDS 服务启动流程 ...