Problem Description
1395. Count Number of Teams
Difficulty: Medium
Problem:
There are n
soldiers standing in a line, each assigned a unique rating value.
You need to form teams of 3 soldiers under the following conditions:
- Choose 3 soldiers with indices
(i, j, k)
where(0 <= i < j < k < n)
. - A team is valid if:
rating[i] < rating[j] < rating[k]
rating[i] > rating[j] > rating[k]
Return the number of teams you can form given the conditions.
Examples:
-
Example 1:
- Input:
rating = [2, 5, 3, 4, 1]
- Output:
3
- Explanation: The valid teams are
(2, 3, 4)
,(5, 4, 1)
, and(5, 3, 1)
.
- Input:
-
Example 2:
- Input:
rating = [2, 1, 3]
- Output:
0
- Explanation: No valid teams can be formed.
- Input:
-
Example 3:
- Input:
rating = [1, 2, 3, 4]
- Output:
4
- Explanation: The valid teams are
(1, 2, 3)
,(1, 2, 4)
,(1, 3, 4)
, and(2, 3, 4)
.
- Input:
Constraints:
n == rating.length
3 <= n <= 1000
1 <= rating[i] <= 10^5
- All integers in
rating
are unique.
Solution
The goal is to optimize the solution to count the number of valid teams. We can reduce the time complexity by using a more efficient approach instead of the triple nested loops.
Optimized Approach:
- Use two auxiliary arrays to keep track of the number of elements less than and greater than each element to the left and right.
- Iterate through each element as the middle element (
mid
). - Count the elements on the left and right that satisfy the conditions.
- Calculate the number of valid teams using these counts.
Here is the optimized code:
from typing import List
class Solution:
def numTeams(self, rating: List[int]) -> int:
n = len(rating)
num = 0
for mid in range(1, n - 1):
littleLeft = 0
greatLeft = 0
littleRight = 0
greatRight = 0
# Count elements on the left
for left in range(mid):
if rating[left] < rating[mid]:
littleLeft += 1
elif rating[left] > rating[mid]:
greatLeft += 1
# Count elements on the right
for right in range(mid + 1, n):
if rating[right] < rating[mid]:
littleRight += 1
elif rating[right] > rating[mid]:
greatRight += 1
# Calculate number of teams
num += littleLeft * greatRight + greatLeft * littleRight
return num
Explanation:
-
Initialization:
- Initialize
num
to store the count of valid teams.
- Initialize
-
Outer Loop (for
mid
):- Iterate through each element as the middle element (
mid
), except the first and last elements.
- Iterate through each element as the middle element (
-
Counting Elements:
- For each
mid
, count elements to the left that are either less or greater than the element atmid
. - Similarly, count elements to the right that are either less or greater than the element at
mid
.
- For each
-
Calculate Teams:
- Use the counts from left and right to calculate the number of valid increasing and decreasing teams and add them to
num
.
- Use the counts from left and right to calculate the number of valid increasing and decreasing teams and add them to
This approach ensures a time complexity of (O(n^2)), making it more efficient for larger input sizes while maintaining clarity and correctness.
Conclusion
The provided solution efficiently counts the number of valid teams that can be formed from the given ratings of soldiers. It leverages counting strategies to reduce the computational complexity compared to a brute-force triple nested loop approach. This method ensures that the solution is optimal and suitable for larger inputs within the given constraints.