返回介绍

solution / 2200-2299 / 2261.K Divisible Elements Subarrays / README_EN

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

2261. K Divisible Elements Subarrays

中文文档

Description

Given an integer array nums and two integers k and p, return _the number of distinct subarrays, which have at most_ k _elements _that are _divisible by_ p.

Two arrays nums1 and nums2 are said to be distinct if:

  • They are of different lengths, or
  • There exists at least one index i where nums1[i] != nums2[i].

A subarray is defined as a non-empty contiguous sequence of elements in an array.

 

Example 1:

Input: nums = [2,3,3,2,2], k = 2, p = 2
Output: 11
Explanation:
The elements at indices 0, 3, and 4 are divisible by p = 2.
The 11 distinct subarrays which have at most k = 2 elements divisible by 2 are:
[2], [2,3], [2,3,3], [2,3,3,2], [3], [3,3], [3,3,2], [3,3,2,2], [3,2], [3,2,2], and [2,2].
Note that the subarrays [2] and [3] occur more than once in nums, but they should each be counted only once.
The subarray [2,3,3,2,2] should not be counted because it has 3 elements that are divisible by 2.

Example 2:

Input: nums = [1,2,3,4], k = 4, p = 1
Output: 10
Explanation:
All element of nums are divisible by p = 1.
Also, every subarray of nums will have at most 4 elements that are divisible by 1.
Since all subarrays are distinct, the total number of subarrays satisfying all the constraints is 10.

 

Constraints:

  • 1 <= nums.length <= 200
  • 1 <= nums[i], p <= 200
  • 1 <= k <= nums.length

 

Follow up:

Can you solve this problem in O(n2) time complexity?

Solutions

Solution 1

class Solution:
  def countDistinct(self, nums: List[int], k: int, p: int) -> int:
    n = len(nums)
    s = set()
    for i in range(n):
      cnt = 0
      for j in range(i, n):
        cnt += nums[j] % p == 0
        if cnt > k:
          break
        s.add(tuple(nums[i : j + 1]))
    return len(s)
class Solution {
  public int countDistinct(int[] nums, int k, int p) {
    int n = nums.length;
    Set<String> s = new HashSet<>();
    for (int i = 0; i < n; ++i) {
      int cnt = 0;
      String t = "";
      for (int j = i; j < n; ++j) {
        if (nums[j] % p == 0 && ++cnt > k) {
          break;
        }
        t += nums[j] + ",";
        s.add(t);
      }
    }
    return s.size();
  }
}
class Solution {
public:
  int countDistinct(vector<int>& nums, int k, int p) {
    unordered_set<string> s;
    int n = nums.size();
    for (int i = 0; i < n; ++i) {
      int cnt = 0;
      string t;
      for (int j = i; j < n; ++j) {
        if (nums[j] % p == 0 && ++cnt > k) {
          break;
        }
        t += to_string(nums[j]) + ",";
        s.insert(t);
      }
    }
    return s.size();
  }
};
func countDistinct(nums []int, k int, p int) int {
  s := map[string]struct{}{}
  for i := range nums {
    cnt, t := 0, ""
    for _, x := range nums[i:] {
      if x%p == 0 {
        cnt++
        if cnt > k {
          break
        }
      }
      t += string(x) + ","
      s[t] = struct{}{}
    }
  }
  return len(s)
}
function countDistinct(nums: number[], k: number, p: number): number {
  const n = nums.length;
  const s = new Set();
  for (let i = 0; i < n; ++i) {
    let cnt = 0;
    let t = '';
    for (let j = i; j < n; ++j) {
      if (nums[j] % p === 0 && ++cnt > k) {
        break;
      }
      t += nums[j].toString() + ',';
      s.add(t);
    }
  }
  return s.size;
}

Solution 2

class Solution:
  def countDistinct(self, nums: List[int], k: int, p: int) -> int:
    n = len(nums)
    s = set()
    for i in range(n):
      cnt = 0
      t = ""
      for x in nums[i:]:
        cnt += x % p == 0
        if cnt > k:
          break
        t += str(x) + ","
        s.add(t)
    return len(s)

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

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

发布评论

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