• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode908.最小差值I|SmallestRangeI

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10610215.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 = 0
Output: 0
Explanation: B = [1]

Example 2:

Input: A = 2
Output: 6
Explanation: B = [2,8]

Example 3:

Input: A = 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

给定一个整数数组 A,对于每个整数 A[i],我们可以选择任意 x 满足 -K <= x <= K,并将 x 加到 A[i] 中。

在此过程之后,我们得到一些数组 B

返回 B 的最大值和 B 的最小值之间可能存在的最小差值。 

示例 1:

输入:A = [1], K = 0
输出:0
解释:B = [1]

示例 2:

输入:A = [0,10], K = 2
输出:6
解释:B = [2,8]

示例 3:

输入:A = [1,3,6], K = 3
输出:0
解释:B = [3,3,3] 或 B = [4,4,4] 

提示:

  1. 1 <= A.length <= 10000
  2. 0 <= A[i] <= 10000
  3. 0 <= K <= 10000

16ms

 

 1 class Solution {
 2     func smallestRangeI(_ A: [Int], _ K: Int) -> Int {
 3         var smallest = A[0], largest = A[0]
 4         for num in A {
 5             if num < smallest { smallest = num }
 6             else if num > largest { largest = num }
 7         }
 8         let diff = largest - smallest
 9         if diff <= 2 * K {
10             return 0
11         } else {
12             return diff - 2 * K
13         }
14     }
15 }

120ms

 1 class Solution {
 2     func smallestRangeI(_ A: [Int], _ K: Int) -> Int {
 3         var ma = Int.min
 4         var mi = Int.max
 5         for num in A {
 6             ma = max(ma,num)
 7             mi = min(mi,num)
 8         }
 9         return (ma-mi) <= 2*K ? 0 : ma-mi-2*K
10     }
11 }

124ms

 1 class Solution {
 2     func smallestRangeI(_ A: [Int], _ K: Int) -> Int {
 3         guard A.count > 1 else {
 4             return 0
 5         }
 6         let mx = A.max()!
 7         let mi = A.min()!
 8         return max(0, mx - mi - K * 2)
 9     }
10 }

124ms
1 class Solution {
2     func smallestRangeI(_ A: [Int], _ K: Int) -> Int {
3         var A = A.sorted()
4         guard let first = A.first, let last = A.last else {
5             return 0
6         }
7         return (last - first - 2 * K) > 0 ? (last - first - 2 * K) : 0
8     }
9 }

Runtime: 140 ms

Memory Usage: 19 MB
 1 class Solution {
 2     //抓住最大值与最大值之间的重点关系,
 3     //因为其他数可以通过加减K靠近极值
 4     func smallestRangeI(_ A: [Int], _ K: Int) -> Int {
 5         //如果数组只有一个数字则返回0
 6         if A.count==1{return 0}
 7         //初始化最大值
 8         var maxNum:Int = A[0]
 9         //初始化最小值
10         var minNum:Int = A[0]
11         //遍历数组
12         for i in 0..<A.count
13         {            
14             if A[i] > maxNum
15             {
16                 maxNum = A[i] 
17             }
18             if A[i] < minNum
19             {
20                 minNum = A[i] 
21             }
22         }
23         if minNum+2*K>=maxNum
24         {
25             return 0
26         }
27         else
28         {
29             return maxNum-minNum-2*K
30         }
31     }
32 }

148ms

1 class Solution {
2     func smallestRangeI(_ A: [Int], _ K: Int) -> Int {
3         return max(0, (A.max() ?? 0) - (A.min() ?? 0) - 2*K)
4     }
5 }

180ms

 1 class Solution {
 2     func smallestRangeI(_ A: [Int], _ K: Int) -> Int {
 3         guard A.count != 1 else {
 4             return 0
 5         }
 6         var max = A[0]
 7         var min = A[0]
 8         for i in A {
 9             max = i > max ? i : max
10             min = i < min ? i : min
11         }
12         var diff = max - min
13         return abs(K * 2) >= diff ? 0 : diff - abs(K * 2)
14     }
15 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
swift5.x访问权限控制发布时间:2022-07-14
下一篇:
Swift 4.2中的随机数API发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap