Skip to content

Commit f26d9cf

Browse files
authored
Create 1395. Count Number of Teams
1 parent 8e7c7fa commit f26d9cf

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

1395. Count Number of Teams

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int numTeams(vector<int>& v) {
4+
int ans = 0, n = v.size();
5+
6+
for (int i = 1; i < n-1; i++) {
7+
int left_smaller = 0, right_greater = 0;
8+
9+
for (int j = 0; j < n; j++) {
10+
if (j < i && v[j] < v[i])
11+
left_smaller++;
12+
else if (j > i && v[j] > v[i])
13+
right_greater++;
14+
}
15+
16+
// Counting valid teams in increasing order
17+
ans += left_smaller * right_greater;
18+
19+
// Counting valid teams in decreasing order
20+
ans += (i - left_smaller) * (n - i - right_greater - 1);
21+
}
22+
23+
return ans;
24+
}
25+
};

0 commit comments

Comments
 (0)