-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCount Number of Teams.cpp
More file actions
40 lines (37 loc) · 1.03 KB
/
Copy pathCount Number of Teams.cpp
File metadata and controls
40 lines (37 loc) · 1.03 KB
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
class Solution {
public:
int numTeams(vector<int>& rating) {
int n=rating.size();
vector<vector<int>>dp(n,vector<int>(3,0)); // dp for increasing
vector<vector<int>>dp1(n,vector<int>(3,0)); // dp1 for decreasing
for(int i=0; i<n; i++) dp[i][0] = 1;
for(int i=0; i<n; i++) dp1[i][0] = 1;
for(int k=1; k<=2; k++){
for(int i=0; i<n; i++){
int count=0;
for(int j = 0; j<i; j++){
if(rating[j] < rating[i]){
count+=dp[j][k-1];
}
}
dp[i][k]=count;
}
}
for(int k=1; k<=2; k++){
for(int i=0; i<n; i++){
int count=0;
for(int j=0; j<i; j++){
if(rating[j] > rating[i]){
count += dp1[j][k-1];
}
}
dp1[i][k] = count;
}
}
int ans=0;
for(int i=0; i<n; i++){
ans+=dp[i][2]+dp1[i][2];
}
return ans;
}
};