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]

Note:

  1. 1 <= A.length <= 10000
  2. 0 <= A[i] <= 10000
  3. 0 <= K <= 10000
 思路:
很朴素的想法,扫描一趟即可知道最大值与最小值,然后再比较一下即可。
int smallestRangeI(vector<int>& A, int K)
{
int minV = A[], maxV = A[];
for(int i = ; i < A.size(); i++)
{
minV = min(minV, A[i]);
maxV = max(maxV, A[i]);
}
return max(((maxV - minV) - * K),);
}

[leetcode-908-Smallest Range I]的更多相关文章

  1. [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 ...

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

  3. 【Leetcode_easy】908. Smallest Range I

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

  4. [LeetCode] 910. Smallest Range II 最小区间之二

    Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and ad ...

  5. [LeetCode] 632. Smallest Range Covering Elements from K Lists 覆盖K个列表元素的最小区间

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...

  6. [leetcode]632. Smallest Range最小范围

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...

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

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

  8. [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 ...

  9. 【leetcode】908. Smallest Range I

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

  10. 解题报告-908. Smallest Range I

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

随机推荐

  1. DBCP数据库连接池的简单使用

    0.DBCP简介      DBCP(DataBase connection pool)数据库连接池是 apache 上的一个Java连接池项目.DBCP通过连接池预先同数据库建立一些连接放在内存中( ...

  2. Scanner(基本用法初学)

    package day01; import java.util.Scanner; public class Case05 { public static void main(String[] args ...

  3. SQLSERVER 使用 ROLLUP 汇总数据,实现分组统计,总计(合计),小计

    版权声明:本文为博主原创文章,未经博主允许不得转载.本人观点或有不当之处,请在评论中及时指正,我会在第一时间内修改. https://blog.csdn.net/aiming66/article/de ...

  4. 1004. Counting Leaves(30)—PAT 甲级

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  5. A - Chess Placing CodeForces - 985A

    You are given a chessboard of size 1 × n. It is guaranteed that n is even. The chessboard is painted ...

  6. Vue使用moment.js处理前端时间格式问题

    使用moment.js处理格式显示问题: 1.首先引入moment.js插件 npm install moment --save 2.入口文件main.js中导入并使用 3.在项目中使用即可:data ...

  7. FLINK流计算拓扑任务代码分析<一>

    我打算以 flink 官方的 例子 <<Monitoring the Wikipedia Edit Stream>> 作为示例,进行 flink 流计算任务 的源码解析说明. ...

  8. 树莓派安装DNSMASQ服务

    功能: DNSMASQ是一款高性能的.小型的DNS服务器软件. * 缓存域名.IP地址,本地缓存解析的地址. * DHCP服务 1.安装 sudo apt-get update sudo apt-ge ...

  9. Python学习 :异常处理

    异常处理 什么是异常处理 - python解释器检测到错误,触发异常(也允许程序员自己触发了异常) - 程序员编写特定的代码,专门用来捕捉这个异常(这段代码与程序逻辑无关,只与异常处理有关) - 如果 ...

  10. Python 爬虫 多进程清洗代理

    利用多线程检测代理网站提供的免费代理是否可用 import requests from lxml import etree import time import multiprocessing def ...