# 1395.Count Number of Teams

**1395.Count Number of Teams**

难度:Medium

> 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
1 <= n <= 200
1 <= rating[i] <= 10^5
```

可以建立一个索引，对每个元素，找出在其后比它大和小的数的索引，然后以每个比它大的数为基础，找出比这个大数更大的数的数目即可。递减以此类推。

```
class Solution {
public:
    int numTeams(vector<int>& rating) {
        vector<vector<int>>great,low;
        for(int i=0;i<rating.size();i++)
        {
            vector<int> tgreat, tlow;
            for(int j=i+1;j<rating.size();j++)
            {
                if(rating[j]>rating[i]) tgreat.push_back(j);
                if(rating[j]<rating[i]) tlow.push_back(j);
            }
            great.push_back(tgreat);
            low.push_back(tlow);
        }
        int res=0;
        for(auto v: great)
        {
        //    cout<<v.size()<<endl;
            if(v.size()>=2 )
            {
                for(auto i : v)
                res+=great[i].size();
            }
        }
               for(auto v: low)
        {
         //   cout<<v.size()<<endl;
            if(v.size()>=2 )
            {
                for(auto i : v)
                res+=low[i].size();
            }
        }
        return res;


    }
};
```

> 执行用时 :32 ms, 在所有 C++ 提交中击败了67.70%的用户\
> 内存消耗 :14.2 MB, 在所有 C++ 提交中击败了100.00%的用户


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/1395.count_number_of_teams.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
