-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1395-numTeams.ts
105 lines (86 loc) · 2.45 KB
/
1395-numTeams.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* 1395. Count Number of Teams
* {@link https://leetcode.com/problems/count-number-of-teams/ | Link}
*
* Topics: Array | Dynamic Programming | Binary Indexed Tree
*/
/** Dynamic Programming (Tabulation) (WIP) */
export function numTeams(rating: number[]): number {
const n = rating.length
const f: Record<Type, number[][]> = {
asc: Array.from({ length: n }, () => Array(4).fill(0)),
desc: Array.from({ length: n }, () => Array(4).fill(0)),
}
for (let i = 0; i < n; i++) {
f.asc[i][1] = f.desc[i][1] = 1
}
for (const count of [2, 3]) {
for (let i = 0; i < n; i++) {
for (let j = count + 1; i < n; i++) {
if (rating[j] > rating[i]) f.asc[j][count] += f.asc[i][count - 1]
if (rating[j] < rating[i]) f.desc[j][count] += f.desc[i][count - 1]
}
}
}
let res = 0
for (let i = 0; i < n; i++) {
res += f.asc[i][3] + f.desc[i][3]
}
return res
}
/** Dynamic Programming (Memoization) */
export function numTeams2(rating: number[]): number {
const n = rating.length
const f: Record<Type, number[][]> = {
asc: Array.from({ length: n }, () => Array(3).fill(-1)),
desc: Array.from({ length: n }, () => Array(3).fill(-1)),
}
const fn = (i: number, available: number, type: Type) => {
if (!available) return 1
if (f[type][i][available] !== -1) return f[type][i][available]
let res = 0
for (let j = i + 1; j < n; j++) {
if (rating[j] > rating[i]) {
if (type === 'asc') {
res += fn(j, available - 1, 'asc')
}
} else {
if (type === 'desc') {
res += fn(j, available - 1, 'desc')
}
}
}
f[type][i][available] = res
return res
}
let res = 0
for (let i = 0; i < n; i++) {
res += fn(i, 2, 'asc') + fn(i, 2, 'desc')
}
return res
}
type Type = 'asc' | 'desc'
/** Enumerate Middle Element */
export function numTeams3(rating: number[]): number {
let res = 0
const n = rating.length
for (let i = 0; i < n; i++) {
let [l, r] = [0, 0]
for (let j = i - 1; j >= 0; j--) {
if (rating[j] < rating[i]) l++
}
for (let j = i + 1; j < n; j++) {
if (rating[j] > rating[i]) r++
}
res += l * r
res += (i - l) * (n - 1 - i - r)
}
return res
}
/**
* TODO: Add implementation with Binary Indexed Tree
* {@link https://doocs.github.io/leetcode/en/lc/1395/?h=1395#solution-2-binary-indexed-tree }
*/
export function numTeams4(rating: number[]): number {
return 0
}