Count Number of Teams

Medium Topics Company Tags

The array rating contains distinct ratings in lineup order. Count triples of indices i < j < k whose ratings are either strictly increasing or strictly decreasing. An index may participate in multiple triples. Return the total number of valid triples.

Example 1:

Input: rating = [4,3,2]

Output: 1

Explanation: The only triple uses all three entries, and 4 > 3 > 2 makes it a valid decreasing team.

Example 2:

Input: rating = [1,3,2]

Output: 0

Explanation: The only triple has ratings 1, 3, 2, which are neither strictly increasing nor strictly decreasing.

Example 3:

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

Output: 1

Explanation: The only valid team uses indices (0, 2, 3), with ratings 1 < 2 < 3. The other three triples fail both order conditions.

Constraints:

  • 3 <= rating.length <= 1000
  • 1 <= rating[i] <= 100000
  • All ratings are distinct.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.