★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:爲敢(WeiGanTechnologies)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-xkbhczmc-hz.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★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:git
T < lower
, they performed poorly on their diet and lose 1 point; T > upper
, they performed well on their diet and gain 1 point;Return the total number of points the dieter has after all calories.length
days.github
Note that: The total points could be negative.微信
Example 1:spa
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:code
Input: calories = [3,2], k = 2, lower = 0, upper = 1 Output: 1 Explaination: calories[0] + calories[1] > upper, total points = 1.
Example 3:orm
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:htm
1 <= k <= calories.length <= 10^5
0 <= calories[i] <= 20000
0 <= lower <= upper
你的好友是一位健身愛好者。前段日子,他給本身制定了一份健身計劃。如今想請你幫他評估一下這份計劃是否合理。blog
他會有一份計劃消耗的卡路里表,其中 calories[i]
給出了你的這位好友在第 i
天須要消耗的卡路里總量。get
計劃的統計週期一般是 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
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 }
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 }