Given an integer array nums
, return the number of elements that have both a strictly smaller and a strictly greater element appear in nums
.
Example 1:
Input: nums = [11,7,2,15]
Output: 2
Explanation: The element 7 has the element 2 strictly smaller than it and the element 11 strictly greater than it.
Element 11 has element 7 strictly smaller than it and element 15 strictly greater than it.
In total there are 2 elements having both a strictly smaller and a strictly greater element appear in nums
.
Example 2:
Input: nums = [-3,3,3,90]
Output: 2
Explanation: The element 3 has the element -3 strictly smaller than it and the element 90 strictly greater than it.
Since 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 nums
.
Constraints:
1 <= nums.length <= 100
-105 <= nums[i] <= 105
Solution: Min / Max elements
Find min and max of the array, count elements other than those two.
Time complexity: O(n)
Space complexity: O(1)
C++
1 2 3 4 5 6 7 8 9 10 |
// Author: Huahua class Solution { public: int countElements(vector<int>& nums) { auto [it1, it2] = minmax_element(begin(nums), end(nums)); return count_if(begin(nums), end(nums), [lo=*it1, hi=*it2](int x) { return x != lo && x != hi; }); } }; |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment