在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ A dieter consumes
Return the total number of points the dieter has after all 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: 输入: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.
提示:
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 }
|
请发表评论