Skip to content

Commit b0ed525

Browse files
committed
feat: add ts solution to lc problem: No.1395
1 parent e14fade commit b0ed525

File tree

3 files changed

+154
-1
lines changed

3 files changed

+154
-1
lines changed

solution/1300-1399/1395.Count Number of Teams/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,4 +527,60 @@ function numTeams(rating: number[]): number {
527527

528528
<!-- solution:end -->
529529

530+
<!-- solution:start -->
531+
532+
### Solution 3: Recursion + Memoization
533+
534+
<!-- tabs:start -->
535+
536+
#### TypeScript
537+
538+
```ts
539+
function numTeams(rating: number[]): number {
540+
const n = rating.length;
541+
const f: Record<Type, number[][]> = {
542+
asc: Array.from({ length: n }, () => Array(3).fill(-1)),
543+
desc: Array.from({ length: n }, () => Array(3).fill(-1)),
544+
};
545+
546+
const fn = (i: number, available: number, type: Type) => {
547+
if (!available) {
548+
return 1;
549+
}
550+
if (f[type][i][available] !== -1) {
551+
return f[type][i][available];
552+
}
553+
554+
let ans = 0;
555+
for (let j = i + 1; j < n; j++) {
556+
if (rating[j] > rating[i]) {
557+
if (type === 'asc') {
558+
ans += fn(j, available - 1, 'asc');
559+
}
560+
} else {
561+
if (type === 'desc') {
562+
ans += fn(j, available - 1, 'desc');
563+
}
564+
}
565+
}
566+
f[type][i][available] = ans;
567+
568+
return ans;
569+
};
570+
571+
let ans = 0;
572+
for (let i = 0; i < n; i++) {
573+
ans += fn(i, 2, 'asc') + fn(i, 2, 'desc');
574+
}
575+
576+
return ans;
577+
}
578+
579+
type Type = 'asc' | 'desc';
580+
```
581+
582+
<!-- tabs:end -->
583+
584+
<!-- solution:end -->
585+
530586
<!-- problem:end -->

solution/1300-1399/1395.Count Number of Teams/README_EN.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ tags:
3737
<pre>
3838
<strong>Input:</strong> rating = [2,5,3,4,1]
3939
<strong>Output:</strong> 3
40-
<strong>Explanation:</strong> We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
40+
<strong>Explanation:</strong> We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
4141
</pre>
4242

4343
<p><strong class="example">Example 2:</strong></p>
@@ -525,4 +525,60 @@ function numTeams(rating: number[]): number {
525525

526526
<!-- solution:end -->
527527

528+
<!-- solution:start -->
529+
530+
### Solution 3: Recursion + Memoization
531+
532+
<!-- tabs:start -->
533+
534+
#### TypeScript
535+
536+
```ts
537+
function numTeams(rating: number[]): number {
538+
const n = rating.length;
539+
const f: Record<Type, number[][]> = {
540+
asc: Array.from({ length: n }, () => Array(3).fill(-1)),
541+
desc: Array.from({ length: n }, () => Array(3).fill(-1)),
542+
};
543+
544+
const fn = (i: number, available: number, type: Type) => {
545+
if (!available) {
546+
return 1;
547+
}
548+
if (f[type][i][available] !== -1) {
549+
return f[type][i][available];
550+
}
551+
552+
let ans = 0;
553+
for (let j = i + 1; j < n; j++) {
554+
if (rating[j] > rating[i]) {
555+
if (type === 'asc') {
556+
ans += fn(j, available - 1, 'asc');
557+
}
558+
} else {
559+
if (type === 'desc') {
560+
ans += fn(j, available - 1, 'desc');
561+
}
562+
}
563+
}
564+
f[type][i][available] = ans;
565+
566+
return ans;
567+
};
568+
569+
let ans = 0;
570+
for (let i = 0; i < n; i++) {
571+
ans += fn(i, 2, 'asc') + fn(i, 2, 'desc');
572+
}
573+
574+
return ans;
575+
}
576+
577+
type Type = 'asc' | 'desc';
578+
```
579+
580+
<!-- tabs:end -->
581+
582+
<!-- solution:end -->
583+
528584
<!-- problem:end -->
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function numTeams(rating: number[]): number {
2+
const n = rating.length;
3+
const f: Record<Type, number[][]> = {
4+
asc: Array.from({ length: n }, () => Array(3).fill(-1)),
5+
desc: Array.from({ length: n }, () => Array(3).fill(-1)),
6+
};
7+
8+
const fn = (i: number, available: number, type: Type) => {
9+
if (!available) {
10+
return 1;
11+
}
12+
if (f[type][i][available] !== -1) {
13+
return f[type][i][available];
14+
}
15+
16+
let ans = 0;
17+
for (let j = i + 1; j < n; j++) {
18+
if (rating[j] > rating[i]) {
19+
if (type === 'asc') {
20+
ans += fn(j, available - 1, 'asc');
21+
}
22+
} else {
23+
if (type === 'desc') {
24+
ans += fn(j, available - 1, 'desc');
25+
}
26+
}
27+
}
28+
f[type][i][available] = ans;
29+
30+
return ans;
31+
};
32+
33+
let ans = 0;
34+
for (let i = 0; i < n; i++) {
35+
ans += fn(i, 2, 'asc') + fn(i, 2, 'desc');
36+
}
37+
38+
return ans;
39+
}
40+
41+
type Type = 'asc' | 'desc';

0 commit comments

Comments
 (0)