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:
i
, j
, k
) with rating (rating[i]
, rating[j]
, rating[k]
).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).
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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy for counting teams is about checking every possible combination of soldiers. We explore all groups of three soldiers to see if they meet the team criteria by comparing their ratings.
Here's how the algorithm would work step-by-step:
def count_number_of_teams(ratings):
number_of_teams = 0
for first_soldier_index in range(len(ratings)):
for second_soldier_index in range(first_soldier_index + 1, len(ratings)):
for third_soldier_index in range(second_soldier_index + 1, len(ratings)):
# We now have a possible team of three soldiers
first_soldier_rating = ratings[first_soldier_index]
second_soldier_rating = ratings[second_soldier_index]
third_soldier_rating = ratings[third_soldier_index]
# Check for increasing order
if first_soldier_rating < second_soldier_rating < third_soldier_rating:
number_of_teams += 1
# Check for decreasing order
elif first_soldier_rating > second_soldier_rating > third_soldier_rating:
number_of_teams += 1
return number_of_teams
To count the number of teams, we'll consider each soldier as the middle person on a potential team. For each soldier, we'll count how many soldiers to their left are smaller and how many are larger, and similarly count the smaller and larger soldiers to their right. The number of teams that soldier can be a part of is the product of those counts.
Here's how the algorithm would work step-by-step:
def count_number_of_teams(ratings):
number_of_soldiers = len(ratings)
team_count = 0
for middle_soldier in range(1, number_of_soldiers - 1):
smaller_on_left = 0
larger_on_left = 0
smaller_on_right = 0
larger_on_right = 0
# Count soldiers to the left.
for left_soldier in range(middle_soldier):
if ratings[left_soldier] < ratings[middle_soldier]:
smaller_on_left += 1
elif ratings[left_soldier] > ratings[middle_soldier]:
larger_on_left += 1
# Count soldiers to the right.
for right_soldier in range(middle_soldier + 1, number_of_soldiers):
if ratings[right_soldier] < ratings[middle_soldier]:
smaller_on_right += 1
elif ratings[right_soldier] > ratings[middle_soldier]:
larger_on_right += 1
# Teams can be formed in two ways.
team_count += smaller_on_left * larger_on_right
team_count += larger_on_left * smaller_on_right
return team_count
Case | How to Handle |
---|---|
Empty or null rating array | Return 0 immediately as no team can be formed. |
Rating array with fewer than 3 elements | Return 0 immediately as a team requires 3 soldiers. |
Rating array with all identical values | The result should be 0 since there cannot be a strictly increasing or decreasing sequence. |
Rating array with a sorted increasing sequence | Test case will return (n choose 3) where n is the array length, which can be large. |
Rating array with a sorted decreasing sequence | Test case will return (n choose 3) where n is the array length, which can be large. |
Rating array with large numbers that might cause integer overflow if multiplied | Ensure calculations are performed with a data type that prevents overflow (e.g., long). |
Rating array with duplicate values | The comparisons should still function correctly, counting valid teams even with duplicate skill levels at different indices. |
Maximum size rating array to test performance | Verify the algorithm scales efficiently (e.g., O(n^2) or better) to avoid exceeding time limits. |