返回介绍

solution / 2100-2199 / 2148.Count Elements With Strictly Smaller and Greater Elements / README_EN

发布于 2024-06-17 01:03:09 字数 3519 浏览 0 评论 0 收藏 0

2148. Count Elements With Strictly Smaller and Greater Elements

中文文档

Description

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

Solutions

Solution 1

class Solution:
  def countElements(self, nums: List[int]) -> int:
    mi, mx = min(nums), max(nums)
    return sum(mi < num < mx for num in nums)
class Solution {

  public int countElements(int[] nums) {
    int mi = 1000000, mx = -1000000;
    for (int num : nums) {
      mi = Math.min(mi, num);
      mx = Math.max(mx, num);
    }
    int ans = 0;
    for (int num : nums) {
      if (mi < num && num < mx) {
        ++ans;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int countElements(vector<int>& nums) {
    int mi = 1e6, mx = -1e6;
    for (int num : nums) {
      mi = min(mi, num);
      mx = max(mx, num);
    }
    int ans = 0;
    for (int num : nums)
      if (mi < num && num < mx)
        ++ans;
    return ans;
  }
};
func countElements(nums []int) int {
  mi, mx := int(1e6), -int(1e6)
  for _, num := range nums {
    if num < mi {
      mi = num
    }
    if num > mx {
      mx = num
    }
  }
  ans := 0
  for _, num := range nums {
    if mi < num && num < mx {
      ans++
    }
  }
  return ans
}
function countElements(nums: number[]): number {
  const min = Math.min(...nums),
    max = Math.max(...nums);
  let ans = 0;
  for (let i = 0; i < nums.length; ++i) {
    let cur = nums[i];
    if (cur < max && cur > min) {
      ++ans;
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文