From a79cd82c9b1e710d2a06d5b2190311e7de186420 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Tue, 13 Nov 2018 12:30:01 +0800 Subject: [PATCH] Create 643.cpp --- LeetCode-CN/643.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 LeetCode-CN/643.cpp diff --git a/LeetCode-CN/643.cpp b/LeetCode-CN/643.cpp new file mode 100644 index 0000000..763b229 --- /dev/null +++ b/LeetCode-CN/643.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + double findMaxAverage(vector& nums, int k) { + int n = nums.size(); + int sum = 0; + for (int i = 0; i < k; i++) + { + sum += nums[i]; + } + int current = sum; + int max = current; + for (int begin = 1; begin + k <= n; begin++) + { + current = current - nums[begin - 1] + nums[begin + k - 1]; + if (current > max) + { + max = current; + } + } + return (double)max / k; + } +};