> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Count Number of Teams Python Solution

> Tested Python solution for LeetCode 1395 with 17 pytest cases. Generate a practice environment with lcpy.

LeetCode 1395, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming), [Binary Indexed Tree](/catalog/topics/binary-indexed-tree), [Segment Tree](/catalog/topics/segment-tree). [View on LeetCode](https://leetcode.com/problems/count-number-of-teams/description/).

Generate this problem as a practice environment: tested reference solution, 17 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1395   # by problem number
lcpy gen -s count_number_of_teams   # by problem name
```

## Problem

There are `n` soldiers standing in a line. Each soldier is assigned a **unique** `rating` value.

You have to form a team of 3 soldiers amongst them under the following rules:

* Choose 3 soldiers with index (`i`, `j`, `k`) with rating (`rating[i]`, `rating[j]`, `rating[k]`).
* A team is valid if: `rating[i] < rating[j] < rating[k]` or `rating[i] > rating[j] > rating[k]` where (`0 <= i < j < k < n`).

Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).

### Examples

```
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
```

```
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
```

```
Input: rating = [1,2,3,4]
Output: 4
```

### Constraints

* `n == rating.length`
* `3 <= n <= 1000`
* `1 <= rating[i] <= 10^5`
* All the integers in `rating` are **unique**.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_number_of_teams/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_number_of_teams/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2)
    # Space: O(1)
    def num_teams(self, rating: list[int]) -> int:
        n = len(rating)
        total = 0
        for mid in range(n):
            less_before = sum(rating[i] < rating[mid] for i in range(mid))
            greater_before = mid - less_before
            less_after = sum(rating[k] < rating[mid] for k in range(mid + 1, n))
            greater_after = n - mid - 1 - less_after
            total += less_before * greater_after + greater_before * less_after
        return total
```

## Complexity

| Time | Space |
| - | - |
| O(n^2) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.