Skip to content

Commit df2de2e

Browse files
authored
feat: add solutions to lc problem: No.3729 (#4811)
1 parent cfa6ed9 commit df2de2e

File tree

7 files changed

+392
-6
lines changed

7 files changed

+392
-6
lines changed

solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/README.md

Lines changed: 132 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,154 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3729.Co
7777
#### Python3
7878

7979
```python
80-
80+
class Solution:
81+
def numGoodSubarrays(self, nums: List[int], k: int) -> int:
82+
cnt = Counter({0: 1})
83+
ans = s = 0
84+
for x in nums:
85+
s = (s + x) % k
86+
ans += cnt[s]
87+
cnt[s] += 1
88+
n = len(nums)
89+
i = 0
90+
while i < n:
91+
j = i + 1
92+
while j < n and nums[j] == nums[i]:
93+
j += 1
94+
m = j - i
95+
for h in range(1, m + 1):
96+
if (h * nums[i]) % k == 0:
97+
ans -= m - h
98+
i = j
99+
return ans
81100
```
82101

83102
#### Java
84103

85104
```java
86-
105+
class Solution {
106+
public long numGoodSubarrays(int[] nums, int k) {
107+
long ans = 0;
108+
int s = 0;
109+
Map<Integer, Integer> cnt = new HashMap<>();
110+
cnt.put(0, 1);
111+
for (int x : nums) {
112+
s = (s + x) % k;
113+
ans += cnt.getOrDefault(s, 0);
114+
cnt.merge(s, 1, Integer::sum);
115+
}
116+
int n = nums.length;
117+
for (int i = 0; i < n;) {
118+
int j = i + 1;
119+
while (j < n && nums[j] == nums[i]) {
120+
++j;
121+
}
122+
int m = j - i;
123+
for (int h = 1; h <= m; ++h) {
124+
if (1L * nums[i] * h % k == 0) {
125+
ans -= (m - h);
126+
}
127+
}
128+
i = j;
129+
}
130+
return ans;
131+
}
132+
}
87133
```
88134

89135
#### C++
90136

91137
```cpp
92-
138+
class Solution {
139+
public:
140+
long long numGoodSubarrays(vector<int>& nums, int k) {
141+
long long ans = 0;
142+
int s = 0;
143+
unordered_map<int, int> cnt;
144+
cnt[0] = 1;
145+
for (int x : nums) {
146+
s = (s + x) % k;
147+
ans += cnt[s]++;
148+
}
149+
int n = nums.size();
150+
for (int i = 0; i < n;) {
151+
int j = i + 1;
152+
while (j < n && nums[j] == nums[i]) {
153+
++j;
154+
}
155+
int m = j - i;
156+
for (int h = 1; h <= m; ++h) {
157+
if (1LL * nums[i] * h % k == 0) {
158+
ans -= (m - h);
159+
}
160+
}
161+
i = j;
162+
}
163+
return ans;
164+
}
165+
};
93166
```
94167
95168
#### Go
96169
97170
```go
171+
func numGoodSubarrays(nums []int, k int) (ans int64) {
172+
s := 0
173+
cnt := map[int]int{0: 1}
174+
for _, x := range nums {
175+
s = (s + x) % k
176+
ans += int64(cnt[s])
177+
cnt[s]++
178+
}
179+
180+
n := len(nums)
181+
for i := 0; i < n; {
182+
j := i + 1
183+
for j < n && nums[j] == nums[i] {
184+
j++
185+
}
186+
m := j - i
187+
for h := 1; h <= m; h++ {
188+
if int64(nums[i])*int64(h)%int64(k) == 0 {
189+
ans -= int64(m - h)
190+
}
191+
}
192+
i = j
193+
}
194+
return
195+
}
196+
```
98197

198+
#### TypeScript
199+
200+
```ts
201+
function numGoodSubarrays(nums: number[], k: number): number {
202+
let ans = 0;
203+
let s = 0;
204+
const cnt = new Map<number, number>();
205+
cnt.set(0, 1);
206+
207+
for (const x of nums) {
208+
s = (s + x) % k;
209+
ans += cnt.get(s) ?? 0;
210+
cnt.set(s, (cnt.get(s) ?? 0) + 1);
211+
}
212+
213+
const n = nums.length;
214+
for (let i = 0; i < n; ) {
215+
let j = i + 1;
216+
while (j < n && nums[j] === nums[i]) ++j;
217+
const m = j - i;
218+
for (let h = 1; h <= m; ++h) {
219+
if ((nums[i] * h) % k === 0) {
220+
ans -= m - h;
221+
}
222+
}
223+
i = j;
224+
}
225+
226+
return ans;
227+
}
99228
```
100229

101230
<!-- tabs:end -->

solution/3700-3799/3729.Count Distinct Subarrays Divisible by K in Sorted Array/README_EN.md

Lines changed: 132 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,154 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3729.Co
7272
#### Python3
7373

7474
```python
75-
75+
class Solution:
76+
def numGoodSubarrays(self, nums: List[int], k: int) -> int:
77+
cnt = Counter({0: 1})
78+
ans = s = 0
79+
for x in nums:
80+
s = (s + x) % k
81+
ans += cnt[s]
82+
cnt[s] += 1
83+
n = len(nums)
84+
i = 0
85+
while i < n:
86+
j = i + 1
87+
while j < n and nums[j] == nums[i]:
88+
j += 1
89+
m = j - i
90+
for h in range(1, m + 1):
91+
if (h * nums[i]) % k == 0:
92+
ans -= m - h
93+
i = j
94+
return ans
7695
```
7796

7897
#### Java
7998

8099
```java
81-
100+
class Solution {
101+
public long numGoodSubarrays(int[] nums, int k) {
102+
long ans = 0;
103+
int s = 0;
104+
Map<Integer, Integer> cnt = new HashMap<>();
105+
cnt.put(0, 1);
106+
for (int x : nums) {
107+
s = (s + x) % k;
108+
ans += cnt.getOrDefault(s, 0);
109+
cnt.merge(s, 1, Integer::sum);
110+
}
111+
int n = nums.length;
112+
for (int i = 0; i < n;) {
113+
int j = i + 1;
114+
while (j < n && nums[j] == nums[i]) {
115+
++j;
116+
}
117+
int m = j - i;
118+
for (int h = 1; h <= m; ++h) {
119+
if (1L * nums[i] * h % k == 0) {
120+
ans -= (m - h);
121+
}
122+
}
123+
i = j;
124+
}
125+
return ans;
126+
}
127+
}
82128
```
83129

84130
#### C++
85131

86132
```cpp
87-
133+
class Solution {
134+
public:
135+
long long numGoodSubarrays(vector<int>& nums, int k) {
136+
long long ans = 0;
137+
int s = 0;
138+
unordered_map<int, int> cnt;
139+
cnt[0] = 1;
140+
for (int x : nums) {
141+
s = (s + x) % k;
142+
ans += cnt[s]++;
143+
}
144+
int n = nums.size();
145+
for (int i = 0; i < n;) {
146+
int j = i + 1;
147+
while (j < n && nums[j] == nums[i]) {
148+
++j;
149+
}
150+
int m = j - i;
151+
for (int h = 1; h <= m; ++h) {
152+
if (1LL * nums[i] * h % k == 0) {
153+
ans -= (m - h);
154+
}
155+
}
156+
i = j;
157+
}
158+
return ans;
159+
}
160+
};
88161
```
89162
90163
#### Go
91164
92165
```go
166+
func numGoodSubarrays(nums []int, k int) (ans int64) {
167+
s := 0
168+
cnt := map[int]int{0: 1}
169+
for _, x := range nums {
170+
s = (s + x) % k
171+
ans += int64(cnt[s])
172+
cnt[s]++
173+
}
174+
175+
n := len(nums)
176+
for i := 0; i < n; {
177+
j := i + 1
178+
for j < n && nums[j] == nums[i] {
179+
j++
180+
}
181+
m := j - i
182+
for h := 1; h <= m; h++ {
183+
if int64(nums[i])*int64(h)%int64(k) == 0 {
184+
ans -= int64(m - h)
185+
}
186+
}
187+
i = j
188+
}
189+
return
190+
}
191+
```
93192

193+
#### TypeScript
194+
195+
```ts
196+
function numGoodSubarrays(nums: number[], k: number): number {
197+
let ans = 0;
198+
let s = 0;
199+
const cnt = new Map<number, number>();
200+
cnt.set(0, 1);
201+
202+
for (const x of nums) {
203+
s = (s + x) % k;
204+
ans += cnt.get(s) ?? 0;
205+
cnt.set(s, (cnt.get(s) ?? 0) + 1);
206+
}
207+
208+
const n = nums.length;
209+
for (let i = 0; i < n; ) {
210+
let j = i + 1;
211+
while (j < n && nums[j] === nums[i]) ++j;
212+
const m = j - i;
213+
for (let h = 1; h <= m; ++h) {
214+
if ((nums[i] * h) % k === 0) {
215+
ans -= m - h;
216+
}
217+
}
218+
i = j;
219+
}
220+
221+
return ans;
222+
}
94223
```
95224

96225
<!-- tabs:end -->
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
long long numGoodSubarrays(vector<int>& nums, int k) {
4+
long long ans = 0;
5+
int s = 0;
6+
unordered_map<int, int> cnt;
7+
cnt[0] = 1;
8+
for (int x : nums) {
9+
s = (s + x) % k;
10+
ans += cnt[s]++;
11+
}
12+
int n = nums.size();
13+
for (int i = 0; i < n;) {
14+
int j = i + 1;
15+
while (j < n && nums[j] == nums[i]) {
16+
++j;
17+
}
18+
int m = j - i;
19+
for (int h = 1; h <= m; ++h) {
20+
if (1LL * nums[i] * h % k == 0) {
21+
ans -= (m - h);
22+
}
23+
}
24+
i = j;
25+
}
26+
return ans;
27+
}
28+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func numGoodSubarrays(nums []int, k int) (ans int64) {
2+
s := 0
3+
cnt := map[int]int{0: 1}
4+
for _, x := range nums {
5+
s = (s + x) % k
6+
ans += int64(cnt[s])
7+
cnt[s]++
8+
}
9+
10+
n := len(nums)
11+
for i := 0; i < n; {
12+
j := i + 1
13+
for j < n && nums[j] == nums[i] {
14+
j++
15+
}
16+
m := j - i
17+
for h := 1; h <= m; h++ {
18+
if int64(nums[i])*int64(h)%int64(k) == 0 {
19+
ans -= int64(m - h)
20+
}
21+
}
22+
i = j
23+
}
24+
return
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public long numGoodSubarrays(int[] nums, int k) {
3+
long ans = 0;
4+
int s = 0;
5+
Map<Integer, Integer> cnt = new HashMap<>();
6+
cnt.put(0, 1);
7+
for (int x : nums) {
8+
s = (s + x) % k;
9+
ans += cnt.getOrDefault(s, 0);
10+
cnt.merge(s, 1, Integer::sum);
11+
}
12+
int n = nums.length;
13+
for (int i = 0; i < n;) {
14+
int j = i + 1;
15+
while (j < n && nums[j] == nums[i]) {
16+
++j;
17+
}
18+
int m = j - i;
19+
for (int h = 1; h <= m; ++h) {
20+
if (1L * nums[i] * h % k == 0) {
21+
ans -= (m - h);
22+
}
23+
}
24+
i = j;
25+
}
26+
return ans;
27+
}
28+
}

0 commit comments

Comments
 (0)