★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-qnnlltil-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
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]
.git
After this process, we have some array B
.github
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:this
Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8]
Example 3:spa
Input: A = [1,3,6], K = 3 Output: 0 Explanation: B = [3,3,3] or B = [4,4,4]
Note:code
1 <= A.length <= 10000
0 <= A[i] <= 10000
0 <= K <= 10000
給定一個整數數組 A
,對於每一個整數 A[i]
,咱們能夠選擇任意 x
知足 -K <= x <= K
,並將 x
加到 A[i]
中。htm
在此過程以後,咱們獲得一些數組 B
。blog
返回 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 <= A.length <= 10000
0 <= A[i] <= 10000
0 <= K <= 10000
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 }
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
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 }