From 21a4d93a51c45f6366afc59a908e101b9076482c Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Sun, 28 Jul 2024 18:27:27 -0600 Subject: 1395. count number of teams --- count_num_teams.c | 3 +-- count_num_teams.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 count_num_teams.py 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 -#include +#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])) -- cgit v1.2.3