{"id":6969,"date":"2020-06-27T11:10:48","date_gmt":"2020-06-27T18:10:48","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6969"},"modified":"2020-06-27T11:11:20","modified_gmt":"2020-06-27T18:11:20","slug":"leetcode-1491-average-salary-excluding-the-minimum-and-maximum-salary","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1491-average-salary-excluding-the-minimum-and-maximum-salary\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1491. Average Salary Excluding the Minimum and Maximum Salary"},"content":{"rendered":"\n<p>Given an array of&nbsp;<strong>unique<\/strong>&nbsp;integers&nbsp;<code>salary<\/code>&nbsp;where&nbsp;<code>salary[i]<\/code>&nbsp;is the salary of the employee&nbsp;<code>i<\/code>.<\/p>\n\n\n\n<p>Return the average salary of employees excluding the minimum and maximum salary.<\/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> salary = [4000,3000,1000,2000]\n<strong>Output:<\/strong> 2500.00000\n<strong>Explanation: <\/strong>Minimum salary and maximum salary are 1000 and 4000 respectively.\nAverage salary excluding minimum and maximum salary is (2000+3000)\/2= 2500\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> salary = [1000,2000,3000]\n<strong>Output:<\/strong> 2000.00000\n<strong>Explanation: <\/strong>Minimum salary and maximum salary are 1000 and 3000 respectively.\nAverage salary excluding minimum and maximum salary is (2000)\/1= 2000\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> salary = [6000,5000,4000,3000,2000,1000]\n<strong>Output:<\/strong> 3500.00000\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> salary = [8000,9000,2000,3000,6000,1000]\n<strong>Output:<\/strong> 4750.00000\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>3 &lt;= salary.length &lt;= 100<\/code><\/li><li><code>10^3&nbsp;&lt;= salary[i] &lt;= 10^6<\/code><\/li><li><code>salary[i]<\/code>&nbsp;is unique.<\/li><li>Answers within&nbsp;<code>10^-5<\/code>&nbsp;of the actual value will be accepted as correct.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\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  double average(vector<int>& salary) {\n    auto [lit, hit] = minmax_element(begin(salary), end(salary));\n    int sum = accumulate(begin(salary), end(salary), 0);\n    return (sum - *lit - *hit) * 1.0 \/ (salary.size() - 2);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of&nbsp;unique&nbsp;integers&nbsp;salary&nbsp;where&nbsp;salary[i]&nbsp;is the salary of the employee&nbsp;i. Return the average salary of employees excluding the minimum and maximum salary. Example 1: Input: salary&#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,277,222],"class_list":["post-6969","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-average","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6969","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=6969"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6969\/revisions"}],"predecessor-version":[{"id":6971,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6969\/revisions\/6971"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}