-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lc_cntTm.cpp
61 lines (51 loc) · 1.01 KB
/
Lc_cntTm.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
52
53
54
55
56
57
58
59
60
61
// https://leetcode.com/problems/partition-equal-subset-sum/
#include<bits/stdc++.h>
using namespace std;
// O(n^3) solution
// int numTeams(vector<int>& rating) {
// int n=rating.size();
// int c=0;
// for(int i=0; i<n-2; i++){
// for(int j=i+1; j<n-1; j++){
// for(int k=j+1; k<n; k++){
// if((rating[i] < rating[j] && rating[j] < rating[k]) || (rating[i] > rating[j] && rating[j] > rating[k])){
// c++;
// }
// }
// }
// }
// return c;
// }
//O(n^2)
int numTeams(vector<int>& rating) {
int n=rating.size();
int c=0;
for(int i=0; i<n; i++){
int lftSml=0, lftLrg=0;
int rtSml=0, rtlrg=0;
for(int j=0; j<i; j++){
if(rating[j]<rating[i]){
lftSml++;
}
if(rating[j]>rating[i]){
lftLrg++;
}
}
for(int k=i+1; k<n; k++){
if(rating[k]<rating[i]){
rtSml++;
}
if(rating[k]>rating[i]){
rtlrg++;
}
}
c += lftSml*rtlrg+lftLrg*rtSml;
}
return c;
}
int main()
{
vector<int> rating = {1,2,3,4};
cout<<numTeams(rating);
return 0;
}