908. Smallest Range I - LeetCode

Description:

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

Accepted

29,142

Submissions

44,849

Solution:

class Solution {
    public int smallestRangeI(int[] A, int K) {

        if(A==null||A.length==1){

            return 0;
        }

        Arrays.sort(A);

        int second = A[0];

        int first = A[A.length -1];

        if((first - second)<=2*K){

            return 0 ;
        }

        else {

            return first - second - K - K ;
        }

    }
}

原文地址:https://www.cnblogs.com/codingyangmao/p/11571797.html

时间: 2024-10-01 05:08:07

908. Smallest Range I - LeetCode的相关文章

[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 add?x?to?A[i]?(only once). After this process, we have some array?B. Return the smallest possible difference between the maximum value of?B?and the mini

[LeetCode] 632. Smallest Range Covering Elements from K Lists

[LeetCode]632. Smallest Range Covering Elements from K Lists 你有 k 个升序排列的整数数组.找到一个最小区间,使得 k 个列表中的每个列表至少有一个数包含在其中. 我们定义如果 b-a < d-c 或者在 b-a == d-c 时 a < c,则区间 [a,b] 比 [c,d] 小. 示例 1: 输入:[[4,10,15,24,26], [0,9,12,20], [5,18,22,30]] 输出: [20,24] 解释: 列表 1:

一道题目- Find the smallest range that includes at least one number from each of the k lists

You have k lists of sorted integers. Find the smallest range that includes at least one number from each of the k lists. For example, List 1: [4, 10, 15, 24, 26] List 2: [0, 9, 12, 20] List 3: [5, 18, 22, 30] The smallest range here would be [20, 24]

LeetCode 910. Smallest Range II

很有意思的一道数学推理题目, 剪枝以后解法也很简洁.初看貌似需要把每个数跟其他数作比较.但排序以后可以发现情况大大简化:对于任一对元素a[i] < a[j], a[i] - k和a[j] + k 的情况可以排除, 因为会产生比原值更大的差, 所以对于原有数组的最小值min最大值max, (min - k, max + k)的情况可以排除.剩下的三种情况, (min - k, max - k), (min + k, max + k) 和 (min + k, max - k),后两种等价于原值max

lc 632. Smallest Range

https://leetcode.com/problems/smallest-range/description/ 给你k个数组,找一个最小区间[a,b],可以包含k个数组中的数字各至少一个. 滑动窗口题. 对于要求"最短"的题目很适用. points: 1.在扩张右界的时候,一旦碰到合法就停止,但不用记录结果.在收缩左界的时候进行记录(判断). code: import heapq class Solution: def __init__(self): self.a=None sel

Leetcode-908 Smallest Range I(最小差值 I)

1 class Solution 2 { 3 public: 4 int smallestRangeI(vector<int>& A, int K) 5 { 6 int Max = INT_MIN; 7 int Min = INT_MAX; 8 for(auto d:A) 9 { 10 if(d > Max) 11 Max = d; 12 if(d < Min) 13 Min = d; 14 } 15 16 if(Min+2*K>=Max) 17 return 0;

Smallest Range II

2020-01-21 21:43:52 问题描述: 问题求解: 这个题目还是有点难度的,感觉很巧妙也很难想到. 整体的思路如下: 1. 首先原问题等价于 +0 / + 2*K 2. 那么res = Max - Min 3. 不断更新Max,Min期望得到更小的res public int smallestRangeII(int[] A, int K) { int n = A.length; Arrays.sort(A); int max = A[0]; int min = A[0]; for (

378. Kth Smallest Element in a Sorted Matrix

https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/#/solutions http://www.cnblogs.com/EdwardLiu/p/6109080.html Heap : public class Solution { public int kthSmallest(int[][] matrix, int k) { int n = matrix.length; PriorityQueue<Tupl

Search for a Range问题

Search for a Range问题 leetcode java 二分查找 1. 问题描述 Given an array of integers sorted in ascending order, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n).If the target is