• [Swift]LeetCode1176. 健身计划评估 | Diet Plan Performance


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

    A dieter consumes calories[i] calories on the i-th day.  For every consecutive sequence of k days, they look at T, the total calories consumed during that sequence of k days:

    • If T < lower, they performed poorly on their diet and lose 1 point; 
    • If T > upper, they performed well on their diet and gain 1 point;
    • Otherwise, they performed normally and there is no change in points.

    Return the total number of points the dieter has after all calories.length days.

    Note that: The total points could be negative.

    Example 1:

    Input: calories = [1,2,3,4,5], k = 1, lower = 3, upper = 3
    Output: 0
    Explaination: calories[0], calories[1] < lower and calories[3], calories[4] > upper, total points = 0.

    Example 2:

    Input: calories = [3,2], k = 2, lower = 0, upper = 1
    Output: 1
    Explaination: calories[0] + calories[1] > upper, total points = 1.
    

    Example 3:

    Input: calories = [6,5,0,0], k = 2, lower = 1, upper = 5
    Output: 0
    Explaination: calories[0] + calories[1] > upper, calories[2] + calories[3] < lower, total points = 0.
    

    Constraints:

    • 1 <= k <= calories.length <= 10^5
    • 0 <= calories[i] <= 20000
    • 0 <= lower <= upper

    你的好友是一位健身爱好者。前段日子,他给自己制定了一份健身计划。现在想请你帮他评估一下这份计划是否合理。

    他会有一份计划消耗的卡路里表,其中 calories[i] 给出了你的这位好友在第 i 天需要消耗的卡路里总量。

    计划的统计周期通常是 k 天,你需要计算他在每一段连续的 k 天内消耗的总卡路里 T:

    • 如果 T < lower,那么这份计划相对糟糕,并失去 1 分; 
    • 如果 T > upper,那么这份计划相对优秀,并获得 1 分;
    • 否则,这份计划普普通通,分值不做变动。

    请返回统计完所有 calories.length 天后得到的总分作为评估结果。

    注意:总分可能是负数。

    示例 1:

    输入:calories = [1,2,3,4,5], k = 1, lower = 3, upper = 3
    输出:0
    解释:calories[0], calories[1] < lower 而 calories[3], calories[4] > upper, 总分 = 0.

    示例 2:

    输入:calories = [3,2], k = 2, lower = 0, upper = 1
    输出:1
    解释:calories[0] + calories[1] > upper, 总分 = 1.
    

    示例 3:

    输入:calories = [6,5,0,0], k = 2, lower = 1, upper = 5
    输出:0
    解释:calories[0] + calories[1] > upper, calories[2] + calories[3] < lower, 总分 = 0.
    

    提示:

    • 1 <= k <= calories.length <= 10^5
    • 0 <= calories[i] <= 20000
    • 0 <= lower <= upper

    Runtime: 220 ms
    Memory Usage: 22.5 MB
     1 class Solution {
     2     func dietPlanPerformance(_ calories: [Int], _ k: Int, _ lower: Int, _ upper: Int) -> Int {
     3         var n:Int = calories.count
     4         var sum:Int = 0
     5         var ans:Int = 0
     6         for i in 0..<n
     7         {
     8             sum += calories[i]
     9             if i >= k
    10             {
    11                 sum -= calories[i-k]
    12             }
    13             if i >= k-1
    14             {
    15                 if sum > upper
    16                 {
    17                     ans += 1
    18                 }
    19                 else if sum < lower
    20                 {
    21                     ans -= 1
    22                 }
    23             }
    24         }
    25         return ans
    26     }
    27 }

    224ms
     1 class Solution {
     2     func dietPlanPerformance(_ calories: [Int], _ k: Int, _ lower: Int, _ upper: Int) -> Int {
     3         guard calories.count > 0 && k >= 1 else { return 0 }
     4                 
     5         var res = 0
     6         var sum = 0
     7         
     8         var i = -1
     9         var j = 0
    10         for j in 0..<calories.count {
    11             sum += calories[j]
    12 
    13             if j - i > k {
    14                 i += 1
    15                 sum -= calories[i]                
    16             }
    17 
    18             if j - i < k { continue }
    19             
    20             if sum < lower { res -= 1 }
    21             else if sum > upper { res += 1 }   
    22         }
    23         
    24         return res
    25     }
    26 }

    228ms

     1 class Solution {
     2     func dietPlanPerformance(_ calories: [Int], _ k: Int, _ lower: Int, _ upper: Int) -> Int {
     3         
     4         var i = 0 
     5         var result = 0
     6         var sum = 0
     7         while i < calories.count {
     8             sum += calories[i]
     9             if i < k { 
    10                 if i == k - 1 {
    11                     if sum > upper { result += 1 }
    12                     if sum < lower { result -= 1 }
    13                 }
    14                 i += 1
    15                 continue 
    16             } else {
    17                 // print(sum)
    18                 sum -= calories[i - k]
    19             }
    20             if sum > upper { result += 1 }
    21             if sum < lower { result -= 1 }
    22             i += 1
    23         }
    24         return result
    25     }
    26 }

    232ms

     1 class Solution {
     2     func dietPlanPerformance(_ calories: [Int], _ k: Int, _ lower: Int, _ upper: Int) -> Int {
     3         guard calories.count >= k else {
     4             return 0
     5         }
     6         func getScore(_ costCalories:  Int)  -> Int {
     7             if costCalories < lower {
     8                 return -1
     9             }
    10             if  costCalories > upper {
    11                 return 1
    12             }
    13             return 0
    14         }
    15         var ans = 0
    16         var i = 0
    17         var tempCalories = 0
    18         while i < k {
    19             tempCalories += calories[i]
    20             i += 1
    21         }
    22         ans += getScore(tempCalories)
    23         while i < calories.count {
    24             tempCalories += (calories[i] - calories[i -  k])
    25             ans += getScore(tempCalories)
    26             i += 1
    27         }
    28         return ans
    29     }
    30 }

    236ms

     1 class Solution {
     2     func dietPlanPerformance(_ calories: [Int], _ k: Int, _ lower: Int, _ upper: Int) -> Int {
     3         var totalPoints = 0
     4         var preKSum: Int? = nil
     5         for i in k-1 ..< calories.count {
     6             if var kSum = preKSum {
     7                 preKSum = kSum - calories[i-k] + calories[i]
     8             } else {
     9                 preKSum = calories[0..<k].reduce(0,+)
    10             }
    11             
    12             if preKSum! < lower {
    13                 totalPoints -= 1
    14             } else if preKSum! > upper {
    15                 totalPoints += 1
    16             }
    17         }
    18         
    19         return totalPoints
    20     }
    21 }
  • 相关阅读:
    运动检测技术在数字化监控中的实现和应用(作者:何峻峰)
    EF BB BF的问题
    理解HTTP幂等性
    FusionCharts 分类以及各个属性 参数列表
    SQL语言包含的四个部分
    Inno Setup (安装程序制作)
    PowerDesigner 参照完整性约束(级联删除)
    java默认语法、EL、JSTL表达式,JSTL和struts Tag标签的使用总结
    修改PowerDesigner中create index的bug
    神奇的java Object ( Object和数组关系) Object数据互转
  • 原文地址:https://www.cnblogs.com/strengthen/p/11443476.html
Copyright © 2020-2023  润新知