Python | Brute Force
see the Successfully Accepted Submission\n\n# Code\n\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        count
[see the Successfully Accepted Submission](https://leetcode.com/problems/count-number-of-teams/submissions/1337131003/?envType=daily-question&envId=2024-07-29)\n\n# Code\n```\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        count = 0\n        \n        for j in range(n):\n            left_less = left_greater = right_less = right_greater = 0\n            \n            for i in range(j):\n                if rating[i] < rating[j]:\n                    left_less += 1\n                elif rating[i] > rating[j]:\n                    left_greater += 1\n            \n            for k in range(j + 1, n):\n                if rating[k] < rating[j]:\n                    right_less += 1\n                elif rating[k] > rating[j]:\n                    right_greater += 1\n            \n            count += left_less * right_greater + left_greater * right_less\n        \n        return count\n        \n```\n\n# Steps\n\n1. **Initialize Counters**: For each soldier `j`, maintain two counts:\n   - `left_less[j]`: Number of soldiers to the left of `j` with a rating less than `rating[j]`.\n   - `left_greater[j]`: Number of soldiers to the left of `j` with a rating greater than `rating[j]`.\n   - `right_less[j]`: Number of soldiers to the right of `j` with a rating less than `rating[j]`.\n   - `right_greater[j]`: Number of soldiers to the right of `j` with a rating greater than `rating[j]`.\n\n2. **Count Valid Teams**: For each soldier `j`, the number of valid teams where `rating[j]` is the middle soldier can be found by:\n   - Teams forming increasing sequences: `left_less[j] * right_greater[j]`.\n   - Teams forming decreasing sequences: `left_greater[j] * right_less[j]`.\n\n3. **Sum Up**: Sum all valid teams.\n\n# Explanation of the Code\n\n- **Initialization**: We initialize `n` as the length of the `rating` list and `count` to keep track of valid teams.\n- **Two Nested Loops**: The outer loop iterates through each soldier `j`, and the two inner loops count soldiers to the left and right of `j` that are less or greater in rating.\n- **Counting Valid Teams**: For each `j`, we calculate the number of valid teams where `j` is the middle soldier using the counts from the inner loops.\n- **Sum Up**: The total count of valid teams is returned at the end.\n\n\n![image](https://assets.leetcode.com/users/images/b1e4af48-4da1-486c-bc97-b11ae02febd0_1696186577.992237.jpeg)
['Python3']