返回介绍

solution / 2700-2799 / 2799.Count Complete Subarrays in an Array / README_EN

发布于 2024-06-17 01:02:59 字数 7542 浏览 0 评论 0 收藏 0

2799. Count Complete Subarrays in an Array

中文文档

Description

You are given an array nums consisting of positive integers.

We call a subarray of an array complete if the following condition is satisfied:

  • The number of distinct elements in the subarray is equal to the number of distinct elements in the whole array.

Return _the number of complete subarrays_.

A subarray is a contiguous non-empty part of an array.

 

Example 1:

Input: nums = [1,3,1,2,2]
Output: 4
Explanation: The complete subarrays are the following: [1,3,1,2], [1,3,1,2,2], [3,1,2] and [3,1,2,2].

Example 2:

Input: nums = [5,5,5,5]
Output: 10
Explanation: The array consists only of the integer 5, so any subarray is complete. The number of subarrays that we can choose is 10.

 

Constraints:

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 2000

Solutions

Solution 1

class Solution:
  def countCompleteSubarrays(self, nums: List[int]) -> int:
    cnt = len(set(nums))
    ans, n = 0, len(nums)
    for i in range(n):
      s = set()
      for x in nums[i:]:
        s.add(x)
        if len(s) == cnt:
          ans += 1
    return ans
class Solution {
  public int countCompleteSubarrays(int[] nums) {
    Set<Integer> s = new HashSet<>();
    for (int x : nums) {
      s.add(x);
    }
    int cnt = s.size();
    int ans = 0, n = nums.length;
    for (int i = 0; i < n; ++i) {
      s.clear();
      for (int j = i; j < n; ++j) {
        s.add(nums[j]);
        if (s.size() == cnt) {
          ++ans;
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int countCompleteSubarrays(vector<int>& nums) {
    unordered_set<int> s(nums.begin(), nums.end());
    int cnt = s.size();
    int ans = 0, n = nums.size();
    for (int i = 0; i < n; ++i) {
      s.clear();
      for (int j = i; j < n; ++j) {
        s.insert(nums[j]);
        if (s.size() == cnt) {
          ++ans;
        }
      }
    }
    return ans;
  }
};
func countCompleteSubarrays(nums []int) (ans int) {
  s := map[int]bool{}
  for _, x := range nums {
    s[x] = true
  }
  cnt := len(s)
  for i := range nums {
    s = map[int]bool{}
    for _, x := range nums[i:] {
      s[x] = true
      if len(s) == cnt {
        ans++
      }
    }
  }
  return
}
function countCompleteSubarrays(nums: number[]): number {
  const s: Set<number> = new Set(nums);
  const cnt = s.size;
  const n = nums.length;
  let ans = 0;
  for (let i = 0; i < n; ++i) {
    s.clear();
    for (let j = i; j < n; ++j) {
      s.add(nums[j]);
      if (s.size === cnt) {
        ++ans;
      }
    }
  }
  return ans;
}
use std::collections::HashSet;
impl Solution {
  pub fn count_complete_subarrays(nums: Vec<i32>) -> i32 {
    let mut set: HashSet<&i32> = nums.iter().collect();
    let n = nums.len();
    let m = set.len();
    let mut ans = 0;
    for i in 0..n {
      set.clear();
      for j in i..n {
        set.insert(&nums[j]);
        if set.len() == m {
          ans += n - j;
          break;
        }
      }
    }
    ans as i32
  }
}

Solution 2

class Solution:
  def countCompleteSubarrays(self, nums: List[int]) -> int:
    cnt = len(set(nums))
    d = Counter()
    ans, n = 0, len(nums)
    i = 0
    for j, x in enumerate(nums):
      d[x] += 1
      while len(d) == cnt:
        ans += n - j
        d[nums[i]] -= 1
        if d[nums[i]] == 0:
          d.pop(nums[i])
        i += 1
    return ans
class Solution {
  public int countCompleteSubarrays(int[] nums) {
    Map<Integer, Integer> d = new HashMap<>();
    for (int x : nums) {
      d.put(x, 1);
    }
    int cnt = d.size();
    int ans = 0, n = nums.length;
    d.clear();
    for (int i = 0, j = 0; j < n; ++j) {
      d.merge(nums[j], 1, Integer::sum);
      while (d.size() == cnt) {
        ans += n - j;
        if (d.merge(nums[i], -1, Integer::sum) == 0) {
          d.remove(nums[i]);
        }
        ++i;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int countCompleteSubarrays(vector<int>& nums) {
    unordered_map<int, int> d;
    for (int x : nums) {
      d[x] = 1;
    }
    int cnt = d.size();
    d.clear();
    int ans = 0, n = nums.size();
    for (int i = 0, j = 0; j < n; ++j) {
      d[nums[j]]++;
      while (d.size() == cnt) {
        ans += n - j;
        if (--d[nums[i]] == 0) {
          d.erase(nums[i]);
        }
        ++i;
      }
    }
    return ans;
  }
};
func countCompleteSubarrays(nums []int) (ans int) {
  d := map[int]int{}
  for _, x := range nums {
    d[x] = 1
  }
  cnt := len(d)
  i, n := 0, len(nums)
  d = map[int]int{}
  for j, x := range nums {
    d[x]++
    for len(d) == cnt {
      ans += n - j
      d[nums[i]]--
      if d[nums[i]] == 0 {
        delete(d, nums[i])
      }
      i++
    }
  }
  return
}
function countCompleteSubarrays(nums: number[]): number {
  const d: Map<number, number> = new Map();
  for (const x of nums) {
    d.set(x, (d.get(x) ?? 0) + 1);
  }
  const cnt = d.size;
  d.clear();
  const n = nums.length;
  let ans = 0;
  let i = 0;
  for (let j = 0; j < n; ++j) {
    d.set(nums[j], (d.get(nums[j]) ?? 0) + 1);
    while (d.size === cnt) {
      ans += n - j;
      d.set(nums[i], d.get(nums[i])! - 1);
      if (d.get(nums[i]) === 0) {
        d.delete(nums[i]);
      }
      ++i;
    }
  }
  return ans;
}
use std::collections::HashMap;
use std::collections::HashSet;
impl Solution {
  pub fn count_complete_subarrays(nums: Vec<i32>) -> i32 {
    let n = nums.len();
    let m = nums.iter().collect::<HashSet<&i32>>().len();
    let mut map = HashMap::new();
    let mut ans = 0;
    let mut i = 0;
    for j in 0..n {
      *map.entry(nums[j]).or_insert(0) += 1;
      while map.len() == m {
        ans += n - j;
        let v = map.entry(nums[i]).or_default();
        *v -= 1;
        if *v == 0 {
          map.remove(&nums[i]);
        }
        i += 1;
      }
    }
    ans as i32
  }
}

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

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

发布评论

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