1395. Count Number of Teams LeetCode Solution

In this guide, you will get 1395. Count Number of Teams LeetCode Solution with the best time and space complexity. The solution to Count Number of Teams problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Number of Teams solution in C++
  4. Count Number of Teams solution in Java
  5. Count Number of Teams solution in Python
  6. Additional Resources
1395. Count Number of Teams LeetCode Solution image

Problem Statement of Count Number of Teams

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:

Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] 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

Constraints:

n == rating.length
3 <= n <= 1000
1 <= rating[i] <= 105
All the integers in rating are unique.

Complexity Analysis

  • Time Complexity: O(n^2)
  • Space Complexity: O(1)

1395. Count Number of Teams LeetCode Solution in C++

class Solution {
 public:
  int numTeams(vector<int>& rating) {
    int ans = 0;

    for (int i = 1; i < rating.size() - 1; ++i) {
      // Calculate soldiers on the left with less/greater ratings.
      int leftLess = 0;
      int leftGreater = 0;
      for (int j = 0; j < i; ++j)
        if (rating[j] < rating[i])
          ++leftLess;
        else if (rating[j] > rating[i])
          ++leftGreater;
      // Calculate soldiers on the right with less/greater ratings.
      int rightLess = 0;
      int rightGreater = 0;
      for (int j = i + 1; j < rating.size(); ++j)
        if (rating[j] < rating[i])
          ++rightLess;
        else if (rating[j] > rating[i])
          ++rightGreater;
      ans += leftLess * rightGreater + leftGreater * rightLess;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1395. Count Number of Teams LeetCode Solution in Java

class Solution {
  public int numTeams(int[] rating) {
    int ans = 0;

    for (int i = 1; i < rating.length - 1; ++i) {
      // Calculate soldiers on the left with less/greater ratings.
      int leftLess = 0;
      int leftGreater = 0;
      for (int j = 0; j < i; ++j)
        if (rating[j] < rating[i])
          ++leftLess;
        else if (rating[j] > rating[i])
          ++leftGreater;
      // Calculate soldiers on the right with less/greater ratings.
      int rightLess = 0;
      int rightGreater = 0;
      for (int j = i + 1; j < rating.length; ++j)
        if (rating[j] < rating[i])
          ++rightLess;
        else if (rating[j] > rating[i])
          ++rightGreater;
      ans += leftLess * rightGreater + leftGreater * rightLess;
    }

    return ans;
  }
}
// code provided by PROGIEZ

1395. Count Number of Teams LeetCode Solution in Python

class Solution:
  def numTeams(self, rating: list[int]) -> int:
    ans = 0

    for i in range(1, len(rating) - 1):
      # Calculate soldiers on the left with less//greater ratings.
      leftLess = 0
      leftGreater = 0
      for j in range(i):
        if rating[j] < rating[i]:
          leftLess += 1
        elif rating[j] > rating[i]:
          leftGreater += 1
      # Calculate soldiers on the right with less//greater ratings.
      rightLess = 0
      rightGreater = 0
      for j in range(i + 1, len(rating)):
        if rating[j] < rating[i]:
          rightLess += 1
        elif rating[j] > rating[i]:
          rightGreater += 1
      ans += leftLess * rightGreater + leftGreater * rightLess

    return ans
# code by PROGIEZ

Additional Resources

See also  581. Shortest Unsorted Continuous Subarray LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.