zoukankan      html  css  js  c++  java
  • [Swift]LeetCode908. 最小差值 I | Smallest Range I

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/9697935.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].

    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]

    给定一个整数数组 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]

    20ms
     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 }

    28ms

     1 class Solution {
     2     func smallestRangeI(_ A: [Int], _ K: Int) -> Int {
     3         var maxA = Int.min
     4         var minA = Int.max
     5         for a in A {
     6             if a > maxA {maxA = a}
     7             if a < minA {minA = a}
     8         }
     9         if maxA - minA <= 2 * K {
    10             return 0
    11         }
    12         return maxA - minA - 2 * K
    13     }
    14 }
    
    
  • 相关阅读:
    第四章:Django模型——添加 Event发布会的表 报错
    第四章:Django模型——admin后台管理
    第四章:Django 模型 —— 设计系统表
    第三章:3.9 清除 Google 浏览器中的缓存
    第三章:3.9 关上窗户
    第三章:3.9 引用Django 认证登陆
    第三章:3.8 登陆 Django 默认后台
    降脂食物
    决定孩子人生高度的,不是知识而是这个!
    百万保险
  • 原文地址:https://www.cnblogs.com/strengthen/p/9697935.html
Copyright © 2011-2022 走看看