Skip to content

Commit 180e482

Browse files
committed
feat: add typescript solution to lc problem: No.2261
No.2261.K Divisible Elements Subarrays
1 parent a1a0d65 commit 180e482

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

solution/2200-2299/2261.K Divisible Elements Subarrays/README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,28 @@ func countDistinct(nums []int, k int, p int) int {
159159
### **TypeScript**
160160

161161
```ts
162-
162+
function countDistinct(nums: number[], k: number, p: number): number {
163+
const n = nums.length;
164+
const numSet = new Set(nums);
165+
const verfiedSet = new Set<number>();
166+
for (let i of numSet) {
167+
if (i % p != 0) continue;
168+
verfiedSet.add(i);
169+
}
170+
let ans = new Set<string>();
171+
for (let i = 0; i < n; i++) {
172+
let sub = [];
173+
for (let j = i, cnt = 0; j < n; j++) {
174+
const num = nums[j];
175+
if (verfiedSet.has(num)) cnt++;
176+
if (cnt > k) break;
177+
sub.push(num);
178+
const str = sub.join(',');
179+
ans.add(str);
180+
}
181+
}
182+
return ans.size;
183+
};
163184
```
164185

165186
### **...**

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,28 @@ func countDistinct(nums []int, k int, p int) int {
149149
### **TypeScript**
150150

151151
```ts
152-
152+
function countDistinct(nums: number[], k: number, p: number): number {
153+
const n = nums.length;
154+
const numSet = new Set(nums);
155+
const verfiedSet = new Set<number>();
156+
for (let i of numSet) {
157+
if (i % p != 0) continue;
158+
verfiedSet.add(i);
159+
}
160+
let ans = new Set<string>();
161+
for (let i = 0; i < n; i++) {
162+
let sub = [];
163+
for (let j = i, cnt = 0; j < n; j++) {
164+
const num = nums[j];
165+
if (verfiedSet.has(num)) cnt++;
166+
if (cnt > k) break;
167+
sub.push(num);
168+
const str = sub.join(',');
169+
ans.add(str);
170+
}
171+
}
172+
return ans.size;
173+
};
153174
```
154175

155176
### **...**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function countDistinct(nums: number[], k: number, p: number): number {
2+
const n = nums.length;
3+
const numSet = new Set(nums);
4+
const verfiedSet = new Set<number>();
5+
for (let i of numSet) {
6+
if (i % p != 0) continue;
7+
verfiedSet.add(i);
8+
}
9+
let ans = new Set<string>();
10+
for (let i = 0; i < n; i++) {
11+
let sub = [];
12+
for (let j = i, cnt = 0; j < n; j++) {
13+
const num = nums[j];
14+
if (verfiedSet.has(num)) cnt++;
15+
if (cnt > k) break;
16+
sub.push(num);
17+
const str = sub.join(',');
18+
ans.add(str);
19+
}
20+
}
21+
return ans.size;
22+
};

0 commit comments

Comments
 (0)