n
名士兵站成一排。每个士兵都有一个 独一无二 的评分 rating
。
每 3 个士兵可以组成一个作战单位,分组规则如下:
- 从队伍中选出下标分别为
i
、j
、k
的 3 名士兵,他们的评分分别为rating[i]
、rating[j]
、rating[k]
- 作战单位需满足:
rating[i] < rating[j] < rating[k]
或者rating[i] > rating[j] > rating[k]
,其中0 <= i < j < k < n
请你返回按上述条件可以组建的作战单位数量。每个士兵都可以是多个作战单位的一部分。
示例 1:
输入:rating = [2,5,3,4,1] 输出:3 解释:我们可以组建三个作战单位 (2,3,4)、(5,4,1)、(5,3,1) 。
示例 2:
输入:rating = [2,1,3] 输出:0 解释:根据题目条件,我们无法组建作战单位。
示例 3:
输入:rating = [1,2,3,4] 输出:4
提示:
n == rating.length
3 <= n <= 1000
1 <= rating[i] <= 10^5
rating
中的元素都是唯一的
外层循环枚举中间节点,内层循环各枚举左右节点,统计个数。
class Solution:
def numTeams(self, rating: List[int]) -> int:
n, ans = len(rating), 0
for j in range(1, n - 1):
ia = ib = ka = kb = 0
for i in range(j):
if rating[i] < rating[j]:
ia += 1
elif rating[i] > rating[j]:
ib += 1
for k in range(j + 1, n):
if rating[j] < rating[k]:
ka += 1
elif rating[j] > rating[k]:
kb += 1
ans += ia * ka + ib * kb
return ans
class Solution {
public int numTeams(int[] rating) {
int n = rating.length;
int ans = 0;
for (int j = 1; j < n - 1; ++j) {
int ia = 0;
int ib = 0;
int ka = 0;
int kb = 0;
for (int i = 0; i < j; ++i) {
if (rating[i] < rating[j]) {
++ia;
} else if (rating[i] > rating[j]) {
++ib;
}
}
for (int k = j + 1; k < n; ++k) {
if (rating[j] < rating[k]) {
++ka;
} else if (rating[j] > rating[k]) {
++kb;
}
}
ans += ia * ka + ib * kb;
}
return ans;
}
}
class Solution {
public:
int numTeams(vector<int>& rating) {
int n = rating.size(), ans = 0;
for (int j = 1; j < n - 1; ++j) {
int ia = 0, ib = 0, ka = 0, kb = 0;
for (int i = 0; i < j; ++i) {
if (rating[i] < rating[j])
++ia;
else if (rating[i] > rating[j])
++ib;
}
for (int k = j + 1; k < n; ++k) {
if (rating[j] < rating[k])
++ka;
else if (rating[j] > rating[k])
++kb;
}
ans += ia * ka + ib * kb;
}
return ans;
}
};
func numTeams(rating []int) int {
n, ans := len(rating), 0
for j := 1; j < n-1; j++ {
ia, ib, ka, kb := 0, 0, 0, 0
for i := 0; i < j; i++ {
if rating[i] < rating[j] {
ia++
} else if rating[i] > rating[j] {
ib++
}
}
for k := j + 1; k < n; k++ {
if rating[j] < rating[k] {
ka++
} else if rating[j] > rating[k] {
kb++
}
}
ans += ia*ka + ib*kb
}
return ans
}