diff options
author | Jack Sangdahl <[email protected]> | 2024-07-28 18:27:27 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-07-28 18:27:27 -0600 |
commit | 21a4d93a51c45f6366afc59a908e101b9076482c (patch) | |
tree | ed8bc29f3789aa11c3d10a7731793569f36e31bb | |
parent | 09570f171a5c9a6540cd7350e0fed3b71b1472ce (diff) | |
download | leetcode-21a4d93a51c45f6366afc59a908e101b9076482c.tar.gz leetcode-21a4d93a51c45f6366afc59a908e101b9076482c.zip |
1395. count number of teams
-rw-r--r-- | count_num_teams.c | 3 | ||||
-rw-r--r-- | count_num_teams.py | 44 |
2 files changed, 45 insertions, 2 deletions
diff --git a/count_num_teams.c b/count_num_teams.c index f8853d5..9f8eb1b 100644 --- a/count_num_teams.c +++ b/count_num_teams.c @@ -1,6 +1,5 @@ // 1395. Count Number of Teams -#include <stdio.h> -#include <stdlib.h> +#include "leetcode.h" /* * there are 'n' soldiers standing in a line. each soldier is assigned a unique diff --git a/count_num_teams.py b/count_num_teams.py new file mode 100644 index 0000000..49d156c --- /dev/null +++ b/count_num_teams.py @@ -0,0 +1,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])) |