{"id":7513,"date":"2020-10-17T13:26:05","date_gmt":"2020-10-17T20:26:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7513"},"modified":"2020-10-17T13:26:31","modified_gmt":"2020-10-17T20:26:31","slug":"leetcode-1619-mean-of-array-after-removing-some-elements","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1619-mean-of-array-after-removing-some-elements\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1619. Mean of Array After Removing Some Elements"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>arr<\/code>, return&nbsp;<em>the mean of the remaining integers after removing the smallest&nbsp;<code>5%<\/code>&nbsp;and the largest&nbsp;<code>5%<\/code>&nbsp;of the elements.<\/em><\/p>\n\n\n\n<p>Answers within&nbsp;<code>10<sup>-5<\/sup><\/code>&nbsp;of the&nbsp;<strong>actual answer<\/strong>&nbsp;will be considered accepted.<\/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> arr = [1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3]\n<strong>Output:<\/strong> 2.00000\n<strong>Explanation:<\/strong> After erasing the minimum and the maximum values of this array, all elements are equal to 2, so the mean is 2.\n<\/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> arr = [6,2,7,5,1,2,0,3,10,2,5,0,5,5,0,8,7,6,8,0]\n<strong>Output:<\/strong> 4.00000\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> arr = [6,0,7,0,7,5,7,8,3,4,0,7,8,1,6,8,1,1,2,4,8,1,9,5,4,3,8,5,10,8,6,6,1,0,6,10,8,2,3,4]\n<strong>Output:<\/strong> 4.77778\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [9,7,8,7,7,8,4,4,6,8,8,7,6,8,8,9,2,6,0,0,1,10,8,6,3,3,5,1,10,9,0,7,10,0,10,4,1,10,6,9,3,6,0,0,2,7,0,6,7,2,9,7,7,3,0,1,6,1,10,3]\n<strong>Output:<\/strong> 5.27778\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [4,8,4,10,0,7,1,3,7,8,8,3,4,1,6,2,1,1,8,0,9,8,0,3,9,10,3,10,1,10,7,3,2,1,4,9,10,7,6,4,0,8,5,1,2,1,6,2,5,0,7,10,9,10,3,7,10,5,8,5,7,6,7,6,10,9,5,10,5,5,7,2,10,7,7,8,2,0,1,1]\n<strong>Output:<\/strong> 5.29167\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>20 &lt;= arr.length &lt;= 1000<\/code><\/li><li><code>arr.length<\/code><strong>is a multiple<\/strong>&nbsp;of&nbsp;<code>20<\/code>.<\/li><li><code>0 &lt;= arr[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sorting<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(nlogn)<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  double trimMean(vector<int>& arr) {\n    sort(begin(arr), end(arr));\n    const int offset = arr.size() \/ 20;\n    const int sum = accumulate(begin(arr) + offset, end(arr) - offset, 0);\n    return static_cast<double>(sum) \/ (arr.size() - 2 * offset);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;arr, return&nbsp;the mean of the remaining integers after removing the smallest&nbsp;5%&nbsp;and the largest&nbsp;5%&nbsp;of the elements. Answers within&nbsp;10-5&nbsp;of the&nbsp;actual answer&nbsp;will be considered accepted.&#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":[20,222,23],"class_list":["post-7513","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7513","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=7513"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7513\/revisions"}],"predecessor-version":[{"id":7515,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7513\/revisions\/7515"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}