{"id":9466,"date":"2022-02-04T16:34:25","date_gmt":"2022-02-05T00:34:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9466"},"modified":"2022-02-04T16:39:12","modified_gmt":"2022-02-05T00:39:12","slug":"leetcode-2148-count-elements-with-strictly-smaller-and-greater-elements","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2148-count-elements-with-strictly-smaller-and-greater-elements\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2148. Count Elements With Strictly Smaller and Greater Elements"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>nums<\/code>, return&nbsp;<em>the number of elements that have&nbsp;<strong>both<\/strong>&nbsp;a strictly smaller and a strictly greater element appear in&nbsp;<\/em><code>nums<\/code>.<\/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> nums = [11,7,2,15]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The element 7 has the element 2 strictly smaller than it and the element 11 strictly greater than it.\nElement 11 has element 7 strictly smaller than it and element 15 strictly greater than it.\nIn total there are 2 elements having both a strictly smaller and a strictly greater element appear in <code>nums<\/code>.\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> nums = [-3,3,3,90]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The element 3 has the element -3 strictly smaller than it and the element 90 strictly greater than it.\nSince there are two elements with the value 3, in total there are 2 elements having both a strictly smaller and a strictly greater element appear in <code>nums<\/code>.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 100<\/code><\/li><li><code>-10<sup>5<\/sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<p>Solution: Min \/ Max elements<\/p>\n\n\n\n<p>Find min and max of the array, count elements other than those two.<\/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 countElements(vector<int>& nums) {\n    auto [it1, it2] = minmax_element(begin(nums), end(nums));\n    return count_if(begin(nums), end(nums), [lo=*it1, hi=*it2](int x) {\n      return x != lo && x != hi;\n    });\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;nums, return&nbsp;the number of elements that have&nbsp;both&nbsp;a strictly smaller and a strictly greater element appear in&nbsp;nums. Example 1: Input: nums = [11,7,2,15]&#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,579],"class_list":["post-9466","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-minmax","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9466","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=9466"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9466\/revisions"}],"predecessor-version":[{"id":9469,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9466\/revisions\/9469"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}