aboutsummaryrefslogtreecommitdiff
path: root/count_num_teams.py
blob: 49d156cfb2243e9ca0885c7ce884fecf2a1ba2a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 1395. 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[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)
"""


class Solution(object):
    def numTeams(self, rating):
        """
        :type rating: List[int]
        :rtype: int
        """
        asc, dsc = 0, 0
        for i, v in enumerate(rating):
            llc, rgc, lgc, rlc = 0, 0, 0, 0
            for l in rating[:i]:
                if l < v:
                    llc += 1
                if l > v:
                    lgc += 1
            for r in rating[i + 1 :]:
                if r > v:
                    rgc += 1
                if r < v:
                    rlc += 1
            asc += llc * rgc
            dsc += lgc * rlc
        return asc + dsc


if __name__ == "__main__":
    obj = Solution()
    print(obj.numTeams(rating=[2, 5, 3, 4, 1]))
    print(obj.numTeams(rating=[2, 1, 3]))
    print(obj.numTeams(rating=[1, 2, 3, 4]))