{"id":3183,"date":"2018-07-16T07:25:48","date_gmt":"2018-07-16T14:25:48","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3183"},"modified":"2018-07-16T07:26:36","modified_gmt":"2018-07-16T14:26:36","slug":"leetcode-643-maximum-average-subarray-i","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-643-maximum-average-subarray-i\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 643. Maximum Average Subarray I"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u627e\u51fak\u957f\u5ea6\u7684\u5b50\u6570\u7ec4\u7684\u5e73\u5747\u503c\u7684\u6700\u5927\u503c\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/maximum-average-subarray-i\/description\/\">https:\/\/leetcode.com\/problems\/maximum-average-subarray-i\/description\/<\/a><\/p>\n<p>Given an array consisting of\u00a0<code>n<\/code>\u00a0integers, find the contiguous subarray of given length\u00a0<code>k<\/code>\u00a0that has the maximum average value. And you need to output the maximum average value.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> [1,12,-5,-6,50,3], k = 4\r\n<b>Output:<\/b> 12.75\r\n<b>Explanation:<\/b> Maximum average is (12-5-6+50)\/4 = 51\/4 = 12.75\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>1 &lt;=\u00a0<code>k<\/code>\u00a0&lt;=\u00a0<code>n<\/code>\u00a0&lt;= 30,000.<\/li>\n<li>Elements of the given array will be in the range [-10,000, 10,000].<\/li>\n<\/ol>\n<h1><strong>Solution: Sliding Window<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 124 ms\r\nclass Solution {\r\npublic:\r\n  double findMaxAverage(vector&lt;int&gt;&amp; nums, int k) {\r\n    const int n = nums.size();\r\n    int sum = 0;\r\n    int ans = INT_MIN;\r\n    for (int i = 0; i &lt; n; ++i) {\r\n      if (i &gt;= k) sum -= nums[i - k];\r\n      sum += nums[i];      \r\n      if (i + 1 &gt;= k) ans = max(ans, sum);      \r\n    }\r\n    return static_cast&lt;double&gt;(ans) \/ k;\r\n  }\r\n};<\/pre>\n<h1><strong>Related\u00a0<\/strong><b>Problems<\/b><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/heap\/leetcode-239-sliding-window-maximum\/\">\u82b1\u82b1\u9171 LeetCode 239. Sliding Window Maximum<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-813-largest-sum-of-averages\/\">\u82b1\u82b1\u9171 LeetCode 813. Largest Sum of Averages<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u627e\u51fak\u957f\u5ea6\u7684\u5b50\u6570\u7ec4\u7684\u5e73\u5747\u503c\u7684\u6700\u5927\u503c\u3002 https:\/\/leetcode.com\/problems\/maximum-average-subarray-i\/description\/ Given an array consisting of\u00a0n\u00a0integers, find the contiguous subarray of given length\u00a0k\u00a0that has the maximum average value. And you need to output&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[277,222,215,41,62],"class_list":["post-3183","post","type-post","status-publish","format-standard","hentry","category-array","tag-average","tag-easy","tag-sliding-window","tag-subarray","tag-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3183","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=3183"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3183\/revisions"}],"predecessor-version":[{"id":3187,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3183\/revisions\/3187"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}