#1395

Count Number of Teams

pupil · 560 · lc medium +28 · verified · 70.2% accepted · 3,448 likes · top 79%

Description

There are n soldiers in a line, each with a unique rating value. Count all valid three-soldier teams (i, j, k) with i < j < k whose ratings form a strictly increasing or strictly decreasing sequence. Soldiers may belong to multiple teams.

Example 1:

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).

Example 2:

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

Example 3:

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

Code

1
2
3