Skip to content

Commit f0ff2b2

Browse files
authored
Create count-number-of-teams.cpp
1 parent 5d87ad6 commit f0ff2b2

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

C++/count-number-of-teams.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(n^2)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int numTeams(vector<int>& rating) {
7+
int result = 0;
8+
for (int i = 1; i < rating.size() - 1; ++i) {
9+
vector<int> less(2), greater(2);
10+
for (int j = 0; j < rating.size(); ++j) {
11+
if (rating[i] > rating[j]) {
12+
++less[i < j];
13+
}
14+
if (rating[i] < rating[j]) {
15+
++greater[i < j];
16+
}
17+
}
18+
result += less[0] * greater[1] + greater[0] * less[1];
19+
}
20+
return result;
21+
}
22+
};

0 commit comments

Comments
 (0)