返回介绍

solution / 0900-0999 / 0992.Subarrays with K Different Integers / README_EN

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

992. Subarrays with K Different Integers

中文文档

Description

Given an integer array nums and an integer k, return _the number of good subarrays of _nums.

A good array is an array where the number of different integers in that array is exactly k.

  • For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.

A subarray is a contiguous part of an array.

 

Example 1:

Input: nums = [1,2,1,2,3], k = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2]

Example 2:

Input: nums = [1,2,1,3,4], k = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].

 

Constraints:

  • 1 <= nums.length <= 2 * 104
  • 1 <= nums[i], k <= nums.length

Solutions

Solution 1

class Solution:
  def subarraysWithKDistinct(self, nums: List[int], k: int) -> int:
    def f(k):
      pos = [0] * len(nums)
      cnt = Counter()
      j = 0
      for i, x in enumerate(nums):
        cnt[x] += 1
        while len(cnt) > k:
          cnt[nums[j]] -= 1
          if cnt[nums[j]] == 0:
            cnt.pop(nums[j])
          j += 1
        pos[i] = j
      return pos

    return sum(a - b for a, b in zip(f(k - 1), f(k)))
class Solution {
  public int subarraysWithKDistinct(int[] nums, int k) {
    int[] left = f(nums, k);
    int[] right = f(nums, k - 1);
    int ans = 0;
    for (int i = 0; i < nums.length; ++i) {
      ans += right[i] - left[i];
    }
    return ans;
  }

  private int[] f(int[] nums, int k) {
    int n = nums.length;
    int[] cnt = new int[n + 1];
    int[] pos = new int[n];
    int s = 0;
    for (int i = 0, j = 0; i < n; ++i) {
      if (++cnt[nums[i]] == 1) {
        ++s;
      }
      for (; s > k; ++j) {
        if (--cnt[nums[j]] == 0) {
          --s;
        }
      }
      pos[i] = j;
    }
    return pos;
  }
}
class Solution {
public:
  int subarraysWithKDistinct(vector<int>& nums, int k) {
    vector<int> left = f(nums, k);
    vector<int> right = f(nums, k - 1);
    int ans = 0;
    for (int i = 0; i < nums.size(); ++i) {
      ans += right[i] - left[i];
    }
    return ans;
  }

  vector<int> f(vector<int>& nums, int k) {
    int n = nums.size();
    vector<int> pos(n);
    int cnt[n + 1];
    memset(cnt, 0, sizeof(cnt));
    int s = 0;
    for (int i = 0, j = 0; i < n; ++i) {
      if (++cnt[nums[i]] == 1) {
        ++s;
      }
      for (; s > k; ++j) {
        if (--cnt[nums[j]] == 0) {
          --s;
        }
      }
      pos[i] = j;
    }
    return pos;
  }
};
func subarraysWithKDistinct(nums []int, k int) (ans int) {
  f := func(k int) []int {
    n := len(nums)
    pos := make([]int, n)
    cnt := make([]int, n+1)
    s, j := 0, 0
    for i, x := range nums {
      cnt[x]++
      if cnt[x] == 1 {
        s++
      }
      for ; s > k; j++ {
        cnt[nums[j]]--
        if cnt[nums[j]] == 0 {
          s--
        }
      }
      pos[i] = j
    }
    return pos
  }
  left, right := f(k), f(k-1)
  for i := range left {
    ans += right[i] - left[i]
  }
  return
}

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

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

发布评论

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