-
Notifications
You must be signed in to change notification settings - Fork 0
/
1395. Count Number of Teams.cpp
51 lines (48 loc) · 1.33 KB
/
1395. Count Number of Teams.cpp
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
#include <vector>
using namespace std;
class Solution {
public:
int numTeams(vector<int>& rating)
{
int sum = 0;
vector<vector<int>> i(rating.size(), vector<int>(4, -1));
vector<vector<int>> d(rating.size(), vector<int>(4, -1));
for (int idx = 0; idx < rating.size(); idx++) {
sum += dfs1(rating, idx, 1, i);
sum += dfs2(rating, idx, 1, d);
}
return sum;
}
int dfs1(vector<int>& A, int i, int j, vector<vector<int>>& team)
{
if (i == A.size())
return 0;
if (j == 3)
return 1;
if (team[i][j] != -1)
return team[i][j];
int sum = 0;
for (int next = i + 1; next < A.size(); next++) {
if (A[next] > A[i]) {
sum += dfs1(A, next, j + 1, team);
}
}
return team[i][j] = sum;
}
int dfs2(vector<int>& A, int i, int j, vector<vector<int>>& team)
{
if (i == A.size())
return 0;
if (j == 3)
return 1;
if (team[i][j] != -1)
return team[i][j];
int sum = 0;
for (int next = i + 1; next < A.size(); next++) {
if (A[next] < A[i]) {
sum += dfs2(A, next, j + 1, team);
}
}
return team[i][j] = sum;
}
};