Skip to content

Commit afa37e8

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

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,54 @@ function numTeams(rating: number[]): number {
579579
type Type = 'asc' | 'desc';
580580
```
581581

582+
#### JavaScript
583+
584+
```js
585+
/**
586+
* @param {number[]} rating
587+
* @return {number}
588+
*/
589+
var numTeams = function (rating) {
590+
const n = rating.length;
591+
const f = {
592+
asc: Array.from({ length: n }, () => Array(3).fill(-1)),
593+
desc: Array.from({ length: n }, () => Array(3).fill(-1)),
594+
};
595+
596+
const fn = (i, available, type) => {
597+
if (!available) {
598+
return 1;
599+
}
600+
if (f[type][i][available] !== -1) {
601+
return f[type][i][available];
602+
}
603+
604+
let ans = 0;
605+
for (let j = i + 1; j < n; j++) {
606+
if (rating[j] > rating[i]) {
607+
if (type === 'asc') {
608+
ans += fn(j, available - 1, 'asc');
609+
}
610+
} else {
611+
if (type === 'desc') {
612+
ans += fn(j, available - 1, 'desc');
613+
}
614+
}
615+
}
616+
f[type][i][available] = ans;
617+
618+
return ans;
619+
};
620+
621+
let ans = 0;
622+
for (let i = 0; i < n; i++) {
623+
ans += fn(i, 2, 'asc') + fn(i, 2, 'desc');
624+
}
625+
626+
return ans;
627+
};
628+
```
629+
582630
<!-- tabs:end -->
583631

584632
<!-- solution:end -->

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,54 @@ function numTeams(rating: number[]): number {
577577
type Type = 'asc' | 'desc';
578578
```
579579

580+
#### JavaScript
581+
582+
```js
583+
/**
584+
* @param {number[]} rating
585+
* @return {number}
586+
*/
587+
var numTeams = function (rating) {
588+
const n = rating.length;
589+
const f = {
590+
asc: Array.from({ length: n }, () => Array(3).fill(-1)),
591+
desc: Array.from({ length: n }, () => Array(3).fill(-1)),
592+
};
593+
594+
const fn = (i, available, type) => {
595+
if (!available) {
596+
return 1;
597+
}
598+
if (f[type][i][available] !== -1) {
599+
return f[type][i][available];
600+
}
601+
602+
let ans = 0;
603+
for (let j = i + 1; j < n; j++) {
604+
if (rating[j] > rating[i]) {
605+
if (type === 'asc') {
606+
ans += fn(j, available - 1, 'asc');
607+
}
608+
} else {
609+
if (type === 'desc') {
610+
ans += fn(j, available - 1, 'desc');
611+
}
612+
}
613+
}
614+
f[type][i][available] = ans;
615+
616+
return ans;
617+
};
618+
619+
let ans = 0;
620+
for (let i = 0; i < n; i++) {
621+
ans += fn(i, 2, 'asc') + fn(i, 2, 'desc');
622+
}
623+
624+
return ans;
625+
};
626+
```
627+
580628
<!-- tabs:end -->
581629

582630
<!-- solution:end -->
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @param {number[]} rating
3+
* @return {number}
4+
*/
5+
var numTeams = function (rating) {
6+
const n = rating.length;
7+
const f = {
8+
asc: Array.from({ length: n }, () => Array(3).fill(-1)),
9+
desc: Array.from({ length: n }, () => Array(3).fill(-1)),
10+
};
11+
12+
const fn = (i, available, type) => {
13+
if (!available) {
14+
return 1;
15+
}
16+
if (f[type][i][available] !== -1) {
17+
return f[type][i][available];
18+
}
19+
20+
let ans = 0;
21+
for (let j = i + 1; j < n; j++) {
22+
if (rating[j] > rating[i]) {
23+
if (type === 'asc') {
24+
ans += fn(j, available - 1, 'asc');
25+
}
26+
} else {
27+
if (type === 'desc') {
28+
ans += fn(j, available - 1, 'desc');
29+
}
30+
}
31+
}
32+
f[type][i][available] = ans;
33+
34+
return ans;
35+
};
36+
37+
let ans = 0;
38+
for (let i = 0; i < n; i++) {
39+
ans += fn(i, 2, 'asc') + fn(i, 2, 'desc');
40+
}
41+
42+
return ans;
43+
};

0 commit comments

Comments
 (0)