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;
18             else
19                 return Max-Min-2*K;
20         }
21 };

原文地址:https://www.cnblogs.com/Asurudo/p/9692228.html

时间: 2024-08-30 14:03:23

Leetcode-908 Smallest Range I(最小差值 I)的相关文章

[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:

[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

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 minim

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

POJ 3264 Balanced Lineup(最大最小差值 线段树水题)

Language: Default Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 37122   Accepted: 17383 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order.

一道题目- 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 Search for Range

class Solution { private: int getDirection(int A[], int idx, int target, bool isDefaultBack) { int r = A[idx] - target; if (r == 0) { r = isDefaultBack ? -1 : 1; } return r; } int getFirstValueIndex(int A[], int n, int target, bool isFromBack) { int

笔试题:排序后取最小差值数

笔试题:要从5个人中选取2个人作为礼仪,其中每个人的身高范围为160-190,要求2个人的身高差值最小(如果差值相同的话,选取其中最高的两人),以升序输出两个人的身高. Smple input:161 189 167 172 188 Sample outPut: 188 189 public class demo06{ public static void main(String[] args){ int[] arr={161,189,167,172,188};//原始值 //将原始值升序排序

leetcode——Evaluate Reverse Polish Notation 求算式值(AC)

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

luogu4234 最小差值生成树

题目大意 在一个带权无向图中,它的最小差值生成树为最大边与最小边差值最小的生成树.求一个图的最小差值生成树. 题解 30分解法 引理1 最小生成树的最大边的边权是所有生成树中最大边边权中的最小值. 证明:任意一棵生成树都可以在最小生成树的基础上,通过不断取一个树外边e,将其替换掉其与生成树所在环中的一条边的方式而得到.我们就看看第一条用来替换的边的情况吧.在不在最小生成树中的边中任取一个边权小于最小生成树最大边m的边e,则e必然与最小生成树的树边形成环.若m不在环中,那么就是替换掉任意一条边,答