diff --git a/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/README.md b/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/README.md index 44f04759603dc..53508e5be3c0b 100644 --- a/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/README.md +++ b/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/README.md @@ -77,25 +77,154 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3729.Co #### Python3 ```python - +class Solution: + def numGoodSubarrays(self, nums: List[int], k: int) -> int: + cnt = Counter({0: 1}) + ans = s = 0 + for x in nums: + s = (s + x) % k + ans += cnt[s] + cnt[s] += 1 + n = len(nums) + i = 0 + while i < n: + j = i + 1 + while j < n and nums[j] == nums[i]: + j += 1 + m = j - i + for h in range(1, m + 1): + if (h * nums[i]) % k == 0: + ans -= m - h + i = j + return ans ``` #### Java ```java - +class Solution { + public long numGoodSubarrays(int[] nums, int k) { + long ans = 0; + int s = 0; + Map cnt = new HashMap<>(); + cnt.put(0, 1); + for (int x : nums) { + s = (s + x) % k; + ans += cnt.getOrDefault(s, 0); + cnt.merge(s, 1, Integer::sum); + } + int n = nums.length; + for (int i = 0; i < n;) { + int j = i + 1; + while (j < n && nums[j] == nums[i]) { + ++j; + } + int m = j - i; + for (int h = 1; h <= m; ++h) { + if (1L * nums[i] * h % k == 0) { + ans -= (m - h); + } + } + i = j; + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long numGoodSubarrays(vector& nums, int k) { + long long ans = 0; + int s = 0; + unordered_map cnt; + cnt[0] = 1; + for (int x : nums) { + s = (s + x) % k; + ans += cnt[s]++; + } + int n = nums.size(); + for (int i = 0; i < n;) { + int j = i + 1; + while (j < n && nums[j] == nums[i]) { + ++j; + } + int m = j - i; + for (int h = 1; h <= m; ++h) { + if (1LL * nums[i] * h % k == 0) { + ans -= (m - h); + } + } + i = j; + } + return ans; + } +}; ``` #### Go ```go +func numGoodSubarrays(nums []int, k int) (ans int64) { + s := 0 + cnt := map[int]int{0: 1} + for _, x := range nums { + s = (s + x) % k + ans += int64(cnt[s]) + cnt[s]++ + } + + n := len(nums) + for i := 0; i < n; { + j := i + 1 + for j < n && nums[j] == nums[i] { + j++ + } + m := j - i + for h := 1; h <= m; h++ { + if int64(nums[i])*int64(h)%int64(k) == 0 { + ans -= int64(m - h) + } + } + i = j + } + return +} +``` +#### TypeScript + +```ts +function numGoodSubarrays(nums: number[], k: number): number { + let ans = 0; + let s = 0; + const cnt = new Map(); + cnt.set(0, 1); + + for (const x of nums) { + s = (s + x) % k; + ans += cnt.get(s) ?? 0; + cnt.set(s, (cnt.get(s) ?? 0) + 1); + } + + const n = nums.length; + for (let i = 0; i < n; ) { + let j = i + 1; + while (j < n && nums[j] === nums[i]) ++j; + const m = j - i; + for (let h = 1; h <= m; ++h) { + if ((nums[i] * h) % k === 0) { + ans -= m - h; + } + } + i = j; + } + + return ans; +} ``` diff --git a/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/README_EN.md b/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/README_EN.md index 5fc92c163458a..d28510a086c28 100644 --- a/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/README_EN.md +++ b/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/README_EN.md @@ -72,25 +72,154 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3729.Co #### Python3 ```python - +class Solution: + def numGoodSubarrays(self, nums: List[int], k: int) -> int: + cnt = Counter({0: 1}) + ans = s = 0 + for x in nums: + s = (s + x) % k + ans += cnt[s] + cnt[s] += 1 + n = len(nums) + i = 0 + while i < n: + j = i + 1 + while j < n and nums[j] == nums[i]: + j += 1 + m = j - i + for h in range(1, m + 1): + if (h * nums[i]) % k == 0: + ans -= m - h + i = j + return ans ``` #### Java ```java - +class Solution { + public long numGoodSubarrays(int[] nums, int k) { + long ans = 0; + int s = 0; + Map cnt = new HashMap<>(); + cnt.put(0, 1); + for (int x : nums) { + s = (s + x) % k; + ans += cnt.getOrDefault(s, 0); + cnt.merge(s, 1, Integer::sum); + } + int n = nums.length; + for (int i = 0; i < n;) { + int j = i + 1; + while (j < n && nums[j] == nums[i]) { + ++j; + } + int m = j - i; + for (int h = 1; h <= m; ++h) { + if (1L * nums[i] * h % k == 0) { + ans -= (m - h); + } + } + i = j; + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long numGoodSubarrays(vector& nums, int k) { + long long ans = 0; + int s = 0; + unordered_map cnt; + cnt[0] = 1; + for (int x : nums) { + s = (s + x) % k; + ans += cnt[s]++; + } + int n = nums.size(); + for (int i = 0; i < n;) { + int j = i + 1; + while (j < n && nums[j] == nums[i]) { + ++j; + } + int m = j - i; + for (int h = 1; h <= m; ++h) { + if (1LL * nums[i] * h % k == 0) { + ans -= (m - h); + } + } + i = j; + } + return ans; + } +}; ``` #### Go ```go +func numGoodSubarrays(nums []int, k int) (ans int64) { + s := 0 + cnt := map[int]int{0: 1} + for _, x := range nums { + s = (s + x) % k + ans += int64(cnt[s]) + cnt[s]++ + } + + n := len(nums) + for i := 0; i < n; { + j := i + 1 + for j < n && nums[j] == nums[i] { + j++ + } + m := j - i + for h := 1; h <= m; h++ { + if int64(nums[i])*int64(h)%int64(k) == 0 { + ans -= int64(m - h) + } + } + i = j + } + return +} +``` +#### TypeScript + +```ts +function numGoodSubarrays(nums: number[], k: number): number { + let ans = 0; + let s = 0; + const cnt = new Map(); + cnt.set(0, 1); + + for (const x of nums) { + s = (s + x) % k; + ans += cnt.get(s) ?? 0; + cnt.set(s, (cnt.get(s) ?? 0) + 1); + } + + const n = nums.length; + for (let i = 0; i < n; ) { + let j = i + 1; + while (j < n && nums[j] === nums[i]) ++j; + const m = j - i; + for (let h = 1; h <= m; ++h) { + if ((nums[i] * h) % k === 0) { + ans -= m - h; + } + } + i = j; + } + + return ans; +} ``` diff --git a/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/Solution.cpp b/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/Solution.cpp new file mode 100644 index 0000000000000..94ffd1b9efb3a --- /dev/null +++ b/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/Solution.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + long long numGoodSubarrays(vector& nums, int k) { + long long ans = 0; + int s = 0; + unordered_map cnt; + cnt[0] = 1; + for (int x : nums) { + s = (s + x) % k; + ans += cnt[s]++; + } + int n = nums.size(); + for (int i = 0; i < n;) { + int j = i + 1; + while (j < n && nums[j] == nums[i]) { + ++j; + } + int m = j - i; + for (int h = 1; h <= m; ++h) { + if (1LL * nums[i] * h % k == 0) { + ans -= (m - h); + } + } + i = j; + } + return ans; + } +}; diff --git a/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/Solution.go b/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/Solution.go new file mode 100644 index 0000000000000..378dbe461ad82 --- /dev/null +++ b/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/Solution.go @@ -0,0 +1,25 @@ +func numGoodSubarrays(nums []int, k int) (ans int64) { + s := 0 + cnt := map[int]int{0: 1} + for _, x := range nums { + s = (s + x) % k + ans += int64(cnt[s]) + cnt[s]++ + } + + n := len(nums) + for i := 0; i < n; { + j := i + 1 + for j < n && nums[j] == nums[i] { + j++ + } + m := j - i + for h := 1; h <= m; h++ { + if int64(nums[i])*int64(h)%int64(k) == 0 { + ans -= int64(m - h) + } + } + i = j + } + return +} diff --git a/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/Solution.java b/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/Solution.java new file mode 100644 index 0000000000000..8308e3021b5a7 --- /dev/null +++ b/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/Solution.java @@ -0,0 +1,28 @@ +class Solution { + public long numGoodSubarrays(int[] nums, int k) { + long ans = 0; + int s = 0; + Map cnt = new HashMap<>(); + cnt.put(0, 1); + for (int x : nums) { + s = (s + x) % k; + ans += cnt.getOrDefault(s, 0); + cnt.merge(s, 1, Integer::sum); + } + int n = nums.length; + for (int i = 0; i < n;) { + int j = i + 1; + while (j < n && nums[j] == nums[i]) { + ++j; + } + int m = j - i; + for (int h = 1; h <= m; ++h) { + if (1L * nums[i] * h % k == 0) { + ans -= (m - h); + } + } + i = j; + } + return ans; + } +} diff --git a/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/Solution.py b/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/Solution.py new file mode 100644 index 0000000000000..0eb877409fac9 --- /dev/null +++ b/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/Solution.py @@ -0,0 +1,20 @@ +class Solution: + def numGoodSubarrays(self, nums: List[int], k: int) -> int: + cnt = Counter({0: 1}) + ans = s = 0 + for x in nums: + s = (s + x) % k + ans += cnt[s] + cnt[s] += 1 + n = len(nums) + i = 0 + while i < n: + j = i + 1 + while j < n and nums[j] == nums[i]: + j += 1 + m = j - i + for h in range(1, m + 1): + if (h * nums[i]) % k == 0: + ans -= m - h + i = j + return ans diff --git a/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/Solution.ts b/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/Solution.ts new file mode 100644 index 0000000000000..a4e123ce510cd --- /dev/null +++ b/solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/Solution.ts @@ -0,0 +1,27 @@ +function numGoodSubarrays(nums: number[], k: number): number { + let ans = 0; + let s = 0; + const cnt = new Map(); + cnt.set(0, 1); + + for (const x of nums) { + s = (s + x) % k; + ans += cnt.get(s) ?? 0; + cnt.set(s, (cnt.get(s) ?? 0) + 1); + } + + const n = nums.length; + for (let i = 0; i < n; ) { + let j = i + 1; + while (j < n && nums[j] === nums[i]) ++j; + const m = j - i; + for (let h = 1; h <= m; ++h) { + if ((nums[i] * h) % k === 0) { + ans -= m - h; + } + } + i = j; + } + + return ans; +}