LeetCode 1395 Count Number of Teams

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).
  • Example 2:

    • Input: rating = [2, 1, 3]
    • Output: 0
    • Explanation: No valid teams can be formed.
  • 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).

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:

  1. Use two auxiliary arrays to keep track of the number of elements less than and greater than each element to the left and right.
  2. Iterate through each element as the middle element (mid).
  3. Count the elements on the left and right that satisfy the conditions.
  4. 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:

  1. Initialization:

    • Initialize num to store the count of valid teams.
  2. Outer Loop (for mid):

    • Iterate through each element as the middle element (mid), except the first and last elements.
  3. Counting Elements:

    • For each mid, count elements to the left that are either less or greater than the element at mid.
    • Similarly, count elements to the right that are either less or greater than the element at mid.
  4. Calculate Teams:

    • Use the counts from left and right to calculate the number of valid increasing and decreasing teams and add them to num.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值