{"id":5514,"date":"2019-08-31T22:22:59","date_gmt":"2019-09-01T05:22:59","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5514"},"modified":"2019-09-01T17:58:58","modified_gmt":"2019-09-02T00:58:58","slug":"leetcode-1176-diet-plan-performance","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-1176-diet-plan-performance\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1176. Diet Plan Performance"},"content":{"rendered":"\n<p>A dieter consumes&nbsp;<code>calories[i]<\/code>&nbsp;calories on the&nbsp;<code>i<\/code>-th day.&nbsp; For every consecutive sequence of&nbsp;<code>k<\/code>&nbsp;days, they look at&nbsp;<em>T<\/em>, the total calories consumed during that sequence of&nbsp;<code>k<\/code>&nbsp;days:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If&nbsp;<code>T &lt; lower<\/code>, they performed poorly on their diet and lose 1 point;&nbsp;<\/li><li>If&nbsp;<code>T &gt; upper<\/code>, they performed well on their diet and gain 1 point;<\/li><li>Otherwise, they performed normally and there is no change in points.<\/li><\/ul>\n\n\n\n<p>Return the total number of points the dieter has after all&nbsp;<code>calories.length<\/code>&nbsp;days.<\/p>\n\n\n\n<p>Note that: The total points could be negative.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> calories = [1,2,3,4,5], k = 1, lower = 3, upper = 3\n<strong>Output:<\/strong> 0\n<strong>Explaination<\/strong>: calories[0], calories[1] &lt; lower and calories[3], calories[4] &gt; upper, total points = 0.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> calories = [3,2], k = 2, lower = 0, upper = 1\n<strong>Output:<\/strong> 1\n<strong>Explaination<\/strong>: calories[0] + calories[1] &gt; upper, total points = 1.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> calories = [6,5,0,0], k = 2, lower = 1, upper = 5\n<strong>Output:<\/strong> 0\n<strong>Explaination<\/strong>: calories[0] + calories[1] &gt; upper, calories[2] + calories[3] &lt; lower, total points = 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= k &lt;= calories.length &lt;= 10^5<\/code><\/li><li><code>0 &lt;= calories[i] &lt;= 20000<\/code><\/li><li><code>0 &lt;= lower &lt;= upper<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sliding Window<\/strong><\/h2>\n\n\n\n<p>Maintain the sum of a sliding window length of k.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int dietPlanPerformance(vector<int>& calories, int k, int lower, int upper) {\n    int ans = 0;\n    int t = accumulate(begin(calories), begin(calories) + k - 1, 0);\n    for (int i = k - 1; i < calories.size(); ++i) {\n      if (i >= k) t -= calories[i - k];\n      t += calories[i];\n      if (t > upper) ++ans;\n      if (t < lower) --ans;      \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A dieter consumes&nbsp;calories[i]&nbsp;calories on the&nbsp;i-th day.&nbsp; For every consecutive sequence of&nbsp;k&nbsp;days, they look at&nbsp;T, the total calories consumed during that sequence of&nbsp;k&nbsp;days: If&nbsp;T &lt; lower,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[476],"tags":[222,215],"class_list":["post-5514","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-easy","tag-sliding-window","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5514"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5514\/revisions"}],"predecessor-version":[{"id":5519,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5514\/revisions\/5519"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}