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

[Swift]LeetCode1176.健身计划评估|DietPlanPerformance

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

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

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

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

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

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 }

 

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Swift中删除Array的元素对象发布时间:2022-07-13
下一篇:
据说,每一个iOSer都想要一张Swift大会门票发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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