description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: return self.increaser(rating, 0, 0) + self.decreaser(rating, 0, pow(10, 5) + 1) def increaser(self, rating, num, last): if num == 3: return 1 if len(rating) == 0: return 0 count = 0 for i in range(len(rating)): if rating[i] > last: count += self.increaser(rating[i + 1 :], num + 1, rating[i]) return count def decreaser(self, rating, num, last): if num == 3: return 1 if len(rating) == 0: return 0 count = 0 for i in range(len(rating)): if rating[i] < last: count += self.decreaser(rating[i + 1 :], num + 1, rating[i]) return count
CLASS_DEF FUNC_DEF VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
from itertools import combinations class Solution: def numTeams(self, rating: List[int]) -> int: res = list(combinations(rating, 3)) count = 0 for i in res: if i[0] > i[1] > i[2] or i[0] < i[1] < i[2]: count += 1 return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: def solve(arr, n, k, arr_max): if k == 0: return 1 if n == len(arr): return 0 if arr[n] > arr_max: return solve(arr, n + 1, k - 1, arr[n]) + solve(arr, n + 1, k, arr_max) else: return solve(arr, n + 1, k, arr_max) return solve(rating, 0, 3, 0) + solve(rating[::-1], 0, 3, 0)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: output = 0 for item in itertools.combinations([x for x in range(len(rating))], 3): if ( rating[item[0]] < rating[item[1]] < rating[item[2]] or rating[item[0]] > rating[item[1]] > rating[item[2]] ): output += 1 return output
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
def look_for_ascending(t_list, sub_array): good_lists = 0 for idx in range(0, len(sub_array)): if sub_array[idx] > t_list[-1]: if len(t_list) == 2: good_lists += 1 else: good_lists += look_for_ascending( t_list + [sub_array[idx]], sub_array[idx + 1 :] ) return good_lists def look_for_descending(t_list, sub_array): good_lists = 0 for idx in range(0, len(sub_array)): if sub_array[idx] < t_list[-1]: if len(t_list) == 2: good_lists += 1 else: good_lists += look_for_descending( t_list + [sub_array[idx]], sub_array[idx + 1 :] ) return good_lists class Solution: def numTeams(self, rating: List[int]) -> int: good = 0 for idx in range(len(rating)): good += look_for_ascending( [rating[idx]], rating[idx + 1 :] ) + look_for_descending([rating[idx]], rating[idx + 1 :]) return good
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: n = len(rating) out = 0 for i in range(1, n - 1): lless = lgreat = rless = rgreat = 0 for j in range(i - 1, -1, -1): if rating[j] > rating[i]: lgreat += 1 if rating[j] < rating[i]: lless += 1 for j in range(i + 1, n): if rating[j] > rating[i]: rgreat += 1 if rating[j] < rating[i]: rless += 1 out += lless * rgreat + lgreat * rless return out
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, s: List[int]) -> int: count = 0 if len(s) < 3: return 0 for i in range(len(s)): for j in range(i + 1, len(s)): for k in range(j + 1, len(s)): if s[i] < s[j] and s[j] < s[k]: count += 1 if s[i] > s[j] and s[j] > s[k]: count += 1 return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: n = len(rating) teams = 0 for i in range(n): for k in range(i + 2, n): if rating[i] < rating[k]: teams += len( [r for r in rating[i + 1 : k] if rating[i] < r < rating[k]] ) else: teams += len( [r for r in rating[i + 1 : k] if rating[i] > r > rating[k]] ) return teams
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: c = 0 for i, v in enumerate(rating[1:-1]): llc = rgc = lgc = rlc = 0 for l in rating[: i + 1]: if l < v: llc += 1 elif l > v: lgc += 1 for r in rating[i + 2 :]: if r > v: rgc += 1 elif r < v: rlc += 1 c += llc * rgc + lgc * rlc return c
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, ratings: List[int]) -> int: teams = [] for i1, r1 in enumerate(ratings): for si2, r2 in enumerate(ratings[i1 + 1 :]): i2 = i1 + 1 + si2 for r3 in ratings[i2 + 1 :]: teams.append((r1, r2, r3)) n = 0 for i, j, k in teams: if i < j < k or i > j > k: n += 1 return n
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: target = 3 self.count = 0 self.lst = [] def isSolution(candidate_count): if candidate_count == target: return True def isViable(rating, i, candidates, candidate_count, direction): if i == 0 or candidate_count == 0: return True if direction == -1 and rating[i] < candidates[-1]: return True if direction == 1 and rating[i] > candidates[-1]: return True return False def backtrack(index, rating, candidates, candidate_count, direction): if isSolution(candidate_count): self.count += 1 return if candidate_count < 3: for i in range(index, len(rating)): if isViable(rating, i, candidates, candidate_count, direction): candidates.append(rating[i]) backtrack( i + 1, rating, candidates, candidate_count + 1, direction ) candidates.pop() directions = [1, -1] for direction in directions: backtrack(0, rating, [], 0, direction) return self.count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR NUMBER RETURN IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR LIST NUMBER VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: cnt = 0 for i in range(1, len(rating) - 1): s_l = 0 s_r = 0 l_l = 0 l_r = 0 for j in range(0, i): if rating[j] < rating[i]: s_l += 1 elif rating[j] > rating[i]: l_l += 1 for k in range(i + 1, len(rating)): if rating[k] > rating[i]: s_r += 1 elif rating[k] < rating[i]: l_r += 1 cnt += s_l * s_r + l_l * l_r return cnt
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: possibleTeams = [] temp = [] for i in range(len(rating)): soldier_i = rating[i] for j in range(i + 1, len(rating)): soldier_j = rating[j] if soldier_j > soldier_i: increasing = True else: increasing = False for k in range(j + 1, len(rating)): soldier_k = rating[k] if increasing and soldier_k > soldier_j: possibleTeams.append((soldier_i, soldier_j, soldier_k)) if increasing == False and soldier_k < soldier_j: possibleTeams.append((soldier_k, soldier_j, soldier_i)) return len(possibleTeams)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rat: List[int]) -> int: if len(rat) < 3: return 0 else: c = 0 for i in range(len(rat)): for j in range(i + 1, len(rat)): for k in range(j + 1, len(rat)): if rat[i] > rat[j] > rat[k]: c += 1 rat.reverse() for i in range(len(rat)): for j in range(i + 1, len(rat)): for k in range(j + 1, len(rat)): if rat[i] > rat[j] > rat[k]: c += 1 return c
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: n = len(rating) up = [0] * n down = [0] * n teams = 0 for i in range(n - 1, -1, -1): for j in range(i + 1, n): if rating[i] < rating[j]: up[i] += 1 teams += up[j] else: down[i] += 1 teams += down[j] return teams
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: def dfs(i: int, prefix: List[int], increasing: bool) -> int: if len(prefix) == 3: return 1 if i == len(rating): return 0 result = 0 rate = rating[i] if increasing and prefix[-1] < rate or not increasing and prefix[-1] > rate: result += dfs(i + 1, prefix + [rate], increasing) result += dfs(i + 1, prefix, increasing) return result result = 0 for i in range(len(rating)): result += dfs(i + 1, [rating[i]], True) + dfs(i + 1, [rating[i]], False) return result
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER LIST VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER LIST VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: ans = 0 n = len(rating) for i in range(n): for j in range(i, n): for k in range(j, n): if rating[i] < rating[j] and rating[j] < rating[k]: ans += 1 rating.reverse() for i in range(n): for j in range(i, n): for k in range(j, n): if rating[i] < rating[j] and rating[j] < rating[k]: ans += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: ratings = [[]] n = len(rating) count = 0 for index_1 in range(0, n): for index_2 in range(index_1 + 1, n): for index_3 in range(index_2 + 1, n): if ( rating[index_1] < rating[index_2] and rating[index_2] < rating[index_3] ): count += 1 for index_1 in range(n - 1, -1, -1): for index_2 in range(index_1 - 1, -1, -1): for index_3 in range(index_2 - 1, -1, -1): if ( rating[index_1] < rating[index_2] and rating[index_2] < rating[index_3] ): count += 1 return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: teams = 0 l = [] x = 0 while x <= len(rating) - 2: y = x + 1 while y <= len(rating) - 1: z = y + 1 while z < len(rating): if rating[x] < rating[y] and rating[y] < rating[z]: teams += 1 l.append([rating[x], rating[y], rating[z]]) elif rating[x] > rating[y] and rating[y] > rating[z]: teams += 1 l.append([rating[x], rating[y], rating[z]]) z += 1 y += 1 x += 1 return teams
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: pairs = [] if len(rating) == 1 or len(rating) == 2: return 0 else: for i in range(len(rating)): for j in range(i + 1, len(rating)): for k in range(j + 1, len(rating)): if rating[i] > rating[j] > rating[k]: pairs.append([rating[i], rating[j], rating[k]]) elif rating[i] < rating[j] < rating[k]: pairs.append([rating[i], rating[j], rating[k]]) return len(pairs)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: count = 0 for i in range(len(rating)): l = [rating[i]] for j in range(i + 1, len(rating)): l.append(rating[j]) for k in range(j + 1, len(rating)): if ( l[0] > l[1] and l[1] > rating[k] or l[0] < l[1] and l[1] < rating[k] ): count += 1 l = l[:1] return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: ans = 0 for i, j, k in combinations(rating, 3): if i < j < k or i > j > k: ans += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, a: List[int]) -> int: greater, lesser = 0, 0 c = 0 n = len(a) for i in range(0, n): for j in range(i + 1, n): if a[i] < a[j]: greater = 0 lesser = 1 elif a[i] > a[j]: lesser = 0 greater = 1 else: continue for k in range(j + 1, n): if a[j] < a[k] and lesser == 1: c = c + 1 elif a[j] > a[k] and greater == 1: c = c + 1 return c
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
from itertools import combinations class Solution: def numTeams(self, rating: List[int]) -> int: result = 0 for tupl in combinations(rating, 3): if tupl[0] < tupl[1] < tupl[2] or tupl[0] > tupl[1] > tupl[2]: result += 1 return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: count = 0 for i in range(len(rating)): map_g = {} map_l = {} for j in range(i + 1, len(rating)): count += sum([(k < rating[j]) for k in list(map_l.keys())]) + sum( [(k > rating[j]) for k in list(map_g.keys())] ) if rating[i] < rating[j]: map_l[rating[j]] = map_l.get(rating[j], 0) + 1 if rating[i] > rating[j]: map_g[rating[j]] = map_g.get(rating[j], 0) + 1 return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: count = 0 size = len(rating) for idx, val in enumerate(rating): for next in range(idx + 1, size): if rating[next] > val: new = next + 1 while new < size: if rating[new] > rating[next]: count += 1 new += 1 elif rating[next] < val: new = next + 1 while new < size: if rating[new] < rating[next]: count += 1 new += 1 return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: count_teams = 0 temp_team = [] all_possible_teams = [] for i in range(len(rating)): for j in range(i, len(rating)): for k in range(j, len(rating)): if i != j and j != k: all_possible_teams.append((rating[i], rating[j], rating[k])) for i in all_possible_teams: if i[0] < i[1] and i[1] < i[2]: count_teams = count_teams + 1 if i[0] > i[1] and i[1] > i[2]: count_teams = count_teams + 1 return count_teams
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, r: List[int]) -> int: if len(r) < 3: return 0 teams = 0 for i in range(0, len(r) - 2): for j in range(i + 1, len(r) - 1): if i >= j: break for k in range(j + 1, len(r)): if j >= k: break if r[i] < r[j] < r[k] or r[i] > r[j] > r[k]: teams += 1 return teams
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: n = len(rating) teams = 0 for i in range(1, n - 1): lt_l, lt_r, gt_l, gt_r = 0, 0, 0, 0 for j in range(n): if rating[i] < rating[j]: if i < j: gt_r += 1 else: gt_l += 1 elif rating[i] > rating[j]: if i < j: lt_r += 1 else: lt_l += 1 teams += lt_l * gt_r + gt_l * lt_r return teams
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: n = len(rating) ans = 0 for i in range(n): for j in range(i + 1, n): if rating[j] > rating[i]: direction = 1 else: direction = 0 for k in range(j + 1, n): if direction and rating[k] > rating[j]: ans += 1 if not direction and rating[k] < rating[j]: ans += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, ratings: List[int]) -> int: res = 0 numRatings = len(ratings) sortedRatings = sorted(ratings) mapping = dict() mapping_rev = dict() mapping[len(ratings)] = dict(list(zip(ratings, [0] * numRatings))) mapping_rev[len(ratings)] = dict(list(zip(ratings, [0] * numRatings))) for index, rating in list(enumerate(ratings))[::-1]: mapping[index] = dict() mapping_rev[index] = dict() for tmpRating in ratings: if tmpRating < rating: mapping[index][tmpRating] = mapping[index + 1][tmpRating] + 1 mapping_rev[index][tmpRating] = mapping_rev[index + 1][tmpRating] else: mapping[index][tmpRating] = mapping[index + 1][tmpRating] mapping_rev[index][tmpRating] = ( mapping_rev[index + 1][tmpRating] + 1 ) for i, rating_i in list(enumerate(ratings)): for j, rating_j in list( dict( list(zip(list(range(i + 1, numRatings + 1)), ratings[i + 1 :])) ).items() ): if rating_j > rating_i: res += mapping[j + 1][rating_j] else: res += mapping_rev[j + 1][rating_j] return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: res = 0 for i in range(1, len(rating) - 1): l_small, l_great = 0, 0 for j in range(i): if rating[i] < rating[j]: l_great += 1 elif rating[i] > rating[j]: l_small += 1 r_small, r_great = 0, 0 for k in range(i + 1, len(rating)): if rating[i] > rating[k]: r_small += 1 elif rating[k] > rating[i]: r_great += 1 res += l_small * r_great res += l_great * r_small return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: l = len(rating) res = 0 for i, r1 in enumerate(rating[: l - 2]): for j in range(i + 1, l - 1): r2 = rating[j] if r1 > r2: for k in range(j + 1, l): r3 = rating[k] if r2 > r3: res += 1 if r1 < r2: for k in range(j + 1, l): r3 = rating[k] if r2 < r3: res += 1 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, A) -> int: L = len(A) result = 0 for j in range(1, L - 1): x, loL, loR, hiL, hiR = A[j], 0, 0, 0, 0 for i in range(j): if A[i] < x: print("x", x) print("A[i]", A[i]) loL += 1 else: hiL += 1 for k in range(j + 1, L): if A[k] < x: loR += 1 else: hiR += 1 result += loL * hiR + hiL * loR print("resut", result) return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def checkList(self, head, tail, init: int) -> int: result = init head_copy = head.copy() for i in head_copy: num = tail[0] ilen = len(i) if ilen > 0: if ilen == 2: if i[0] < i[1] and i[1] < num: result += 1 if i[0] > i[1] and i[1] > num: result += 1 elif ilen == 1: head.append(i + [num]) else: head.append([num]) if len(tail) == 1: return result else: del tail[0] return self.checkList(head, tail, result) def numTeams(self, rating: List[int]) -> int: if len(rating) < 3: return 0 list = [[]] return self.checkList(list, rating, 0)
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR LIST VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST LIST RETURN FUNC_CALL VAR VAR VAR NUMBER VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: n = len(rating) team_sum = 0 teams = [] for i in range(n): for j in range(i + 1, n): for k in range(j + 1, n): if rating[i] < rating[j] < rating[k]: team_sum += 1 if rating[i] > rating[j] > rating[k]: team_sum += 1 return team_sum
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: if len(rating) <= 2: return 0 n = len(rating) cnt = 0 for i in range(n - 2): for j in range(i + 1, n - 1): for k in range(j + 1, n): if ( rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k] ): cnt += 1 return cnt def numTeams1(self, rating: List[int]) -> int: pass
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: table = [ [(1 if rating[i] > rating[j] else 0) for j in range(i + 1, len(rating))] for i in range(0, len(rating) - 1) ] table = table + [[]] count = 0 for i in range(0, len(table)): for j in range(0, len(table[i])): if table[i][j] == 1: count += sum(table[i + 1 + j]) else: count += len(table[i + 1 + j]) - sum(table[i + 1 + j]) return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR LIST LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: unique = [] for i in range(len(rating)): for j in range(i + 1, len(rating)): for k in range(j + 1, len(rating)): if ( rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k] ): unique.append((rating[i], rating[j], rating[k])) return len(unique)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: cnt = 0 for i, x in enumerate(rating): for j, y in enumerate(rating[i + 1 :], i + 1): if y > x: cnt += sum(z > y for z in rating[j + 1 :]) if y < x: cnt += sum(z < y for z in rating[j + 1 :]) return cnt
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: p = 0 res = 0 while p < len(rating): less = sum([(1) for i in range(p) if rating[i] < rating[p]]) more = sum( [(1) for i in range(p + 1, len(rating)) if rating[i] > rating[p]] ) more1 = sum([(1) for i in range(p) if rating[i] > rating[p]]) less1 = sum( [(1) for i in range(p + 1, len(rating)) if rating[i] < rating[p]] ) res += less * more + more1 * less1 p += 1 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: count = 0 for i, s1 in enumerate(rating[:-2]): for j, s2 in enumerate(rating[i + 1 : -1]): if s2 > s1: for s3 in rating[i + j + 2 :]: if s3 > s2: count += 1 else: for s3 in rating[i + j + 2 :]: if s3 < s2: count += 1 return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR FOR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: l = len(rating) res = 0 for i, vi in enumerate(rating): if i >= l - 2: break for j, vj in enumerate(rating[i + 1 :]): if j >= l - 1: break for k, vk in enumerate(rating[i + j + 1 :]): if vi < vj and vj < vk or vi > vj and vj > vk: res += 1 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: teams = 0 a = 0 while a < len(rating) - 2: b = a + 1 while b < len(rating) - 1: c = b + 1 if rating[a] < rating[b]: for c in range(c, len(rating)): if rating[b] < rating[c]: teams += 1 if rating[a] > rating[b]: for c in range(c, len(rating)): if rating[b] > rating[c]: teams += 1 b += 1 a += 1 return teams
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def checkList(self, head: List[List[int]], tail: List[int], init: int) -> int: result = init head_copy = head[:] num = tail[0] for i in head_copy: if len(i) == 2: if i[0] < i[1] and i[1] < num or i[0] > i[1] and i[1] > num: result += 1 else: head.append(i + [num]) if len(tail) == 1: return result else: return self.checkList(head, tail[1:], result) def numTeams(self, rating: List[int]) -> int: if len(rating) < 3: return 0 return self.checkList([[], [rating[0]]], rating[1:], 0)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR NUMBER VAR VAR FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR LIST LIST LIST VAR NUMBER VAR NUMBER NUMBER VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: cnt = 0 for i in rating: for j in rating[rating.index(i) + 1 :]: for k in rating[rating.index(j) + 1 :]: if i < j < k or k < j < i: cnt += 1 return cnt
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
import itertools class Solution: def numTeams(self, rating: List[int]) -> int: ans = 0 for item in itertools.combinations(rating, 3): if item[0] < item[1] and item[1] < item[2]: ans += 1 if item[0] > item[1] and item[1] > item[2]: ans += 1 return ans
IMPORT CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: res = 0 def numTeams(self, rating: List[int]) -> int: def dfs(rating, start, prev): if len(prev) == 3: self.res += 1 return for i in range(start, len(rating)): if not prev or prev[-1] < rating[i]: dfs(rating, i + 1, prev + [rating[i]]) dfs(rating, 0, []) dfs(rating[::-1], 0, []) return self.res
CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER LIST EXPR FUNC_CALL VAR VAR NUMBER NUMBER LIST RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: teams_count = 0 i = 0 while i < len(rating) - 2: j = i + 1 while j < len(rating) - 1: k = j + 1 while k < len(rating): if ( rating[i] < rating[j] and rating[j] < rating[k] or rating[k] < rating[j] and rating[j] < rating[i] ): teams_count += 1 k += 1 j += 1 i += 1 return teams_count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def choose_inc(self, idx, num, gt, stack): if num == 0: self.soln.add(tuple(stack)) return for jidx in range(idx, len(self.rating)): jnum = self.rating[jidx] if jnum > gt: self.choose_inc(jidx + 1, num - 1, jnum, stack + [jnum]) def numTeams(self, rating: List[int]) -> int: self.soln = set() self.rating = rating self.choose_inc(0, 3, 0, []) sol_fwd = list(self.soln) self.soln = set() self.rating.reverse() self.choose_inc(0, 3, 0, []) return len(sol_fwd) + len(self.soln)
CLASS_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR LIST VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER LIST RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: return self.dfs1(rating, []) + self.dfs2(rating, []) def dfs1(self, array, path): if len(path) == 3: return 1 val = 0 for i in range(len(array)): if not path or path[-1] > array[i]: val += self.dfs1(array[i + 1 :], path + [array[i]]) return val def dfs2(self, array, path): if len(path) == 3: return 1 val = 0 for i in range(len(array)): if not path or path[-1] < array[i]: val += self.dfs2(array[i + 1 :], path + [array[i]]) return val
CLASS_DEF FUNC_DEF VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR LIST FUNC_CALL VAR VAR LIST VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR RETURN VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: count = len(rating) if count < 3: return 0 result = 0 for i in range(count - 2): for j in range(i + 1, count - 1): increasing = rating[j] > rating[i] for k in range(j + 1, count): if increasing and rating[k] > rating[j]: result += 1 elif not increasing and rating[k] < rating[j]: result += 1 return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: self.ans = 0 def traverse(idx, arr, direction): if len(arr) == 3: self.ans += 1 return for j in range(idx, len(rating) - (2 - len(arr))): if direction == "up" and rating[j] > arr[-1]: traverse(j + 1, arr.copy() + [rating[j]], direction) if direction == "down" and rating[j] < arr[-1]: traverse(j + 1, arr.copy() + [rating[j]], direction) for i in range(0, len(rating) - 2): traverse(i + 1, [rating[i]], "up") traverse(i + 1, [rating[i]], "down") return self.ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR LIST VAR VAR VAR IF VAR STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER LIST VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER LIST VAR VAR STRING RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
from itertools import combinations class Solution: def numTeams(self, rating: List[int]) -> int: cnt = 0 for c in combinations([i for i in range(len(rating))], 3): i, j, k = c[0], c[1], c[2] if rating[i] < rating[j] and rating[j] < rating[k]: cnt += 1 if rating[i] > rating[j] and rating[j] > rating[k]: cnt += 1 return cnt
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, a: List[int]) -> int: list1 = [] for i in range(len(a) - 2): for j in range(i + 1, len(a) - 1): for k in range(j + 1, len(a)): if a[i] < a[j] < a[k]: list1.append(1) if a[i] > a[j] > a[k]: list1.append(1) return len(list1)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating): self.count = 0 def dfs(soldier, idx): if len(soldier) == 3: if ( soldier[0] < soldier[1] < soldier[2] or soldier[0] > soldier[1] > soldier[2] ): self.count += 1 return for i in range(idx, len(rating)): soldier.append(rating[i]) dfs(soldier, i + 1) soldier.pop() dfs([], 0) return self.count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST NUMBER RETURN VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: stack, ans = [], 0 for r in rating: new = [[r]] for team in stack: if len(team) == 1: new.append(team + [r]) elif len(team) == 2: if team[0] < team[1] < r or team[0] > team[1] > r: ans += 1 stack.extend(new) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR LIST NUMBER FOR VAR VAR ASSIGN VAR LIST LIST VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: ans = 0 for i in range(len(rating) - 2): head = rating[i] for j in range(len(rating) - i - 2): sec = rating[i + j + 1] if sec > head: for k in rating[i + j + 2 :]: if k > sec: ans = ans + 1 elif sec < head: for k in rating[i + j + 2 :]: if k < sec: ans = ans + 1 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR FOR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR FOR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: counter = 0 while len(rating) > 0: r = rating.pop(0) counter += countDown(rating, r, 1) counter += countUp(rating, r, 1) return counter def countUp(rating, value, num_in_team): if len(rating) == 0: return 0 counter = 0 for i, num in enumerate(rating): if num > value and num_in_team == 2: counter += 1 elif num > value: counter += countUp(rating[i:], num, 2) return counter def countDown(rating, value, num_in_team): if len(rating) == 0: return 0 counter = 0 for i, num in enumerate(rating): if num < value and num_in_team == 2: counter += 1 elif num < value: counter += countDown(rating[i:], num, 2) return counter
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: return self.sumHigh(rating) def sumHigh(self, rating: List[int]) -> int: sum = 0 for i in range(len(rating)): for j in range(i, len(rating)): for k in range(j, len(rating)): if rating[i] < rating[j] < rating[k]: sum += 1 if ( rating[len(rating) - 1 - i] < rating[len(rating) - 1 - j] < rating[len(rating) - 1 - k] ): sum += 1 return sum def sumLow(self, rating: List[int]) -> int: sum = 0 for i in range(len(rating) - 1, -1, -1): for j in range(i, -1, -1): for k in range(j, -1, -1): if rating[i] < rating[j] < rating[k]: sum += 1 return sum
CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: cnt = 0 for i, r in enumerate(rating): if i >= len(rating) - 2: break li = [val for val in rating[i + 1 :] if val > r] for j, r2 in enumerate(li): li2 = [val for val in li[j + 1 :] if val > r2] cnt += len(li2) li = [val for val in rating[i + 1 :] if val < r] for j, r2 in enumerate(li): li2 = [val for val in li[j + 1 :] if val < r2] cnt += len(li2) return cnt
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: index = {} n = len(rating) for i in range(n): index[rating[i]] = i rating.sort() count = 0 for i in range(n): for j in range(i + 1, n): if index[rating[i]] < index[rating[j]]: for k in range(j + 1, n): if index[rating[j]] < index[rating[k]]: count += 1 rating.sort(reverse=True) for i in range(n): for j in range(i + 1, n): if index[rating[i]] < index[rating[j]]: for k in range(j + 1, n): if index[rating[j]] < index[rating[k]]: count += 1 return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: ( elm_less_than_cur_to_left, elm_less_than_cur_to_right, elm_greater_than_cur_to_left, elm_greater_than_cur_to_right, ) = ([0] * len(rating), [0] * len(rating), [0] * len(rating), [0] * len(rating)) i = 0 res = 0 while i < len(rating): k = i - 1 while k >= 0: if rating[i] > rating[k]: elm_less_than_cur_to_left[i] += 1 elif rating[i] < rating[k]: elm_greater_than_cur_to_left[i] += 1 k -= 1 k = i + 1 while k < len(rating): if rating[i] > rating[k]: elm_less_than_cur_to_right[i] += 1 elif rating[i] < rating[k]: elm_greater_than_cur_to_right[i] += 1 k += 1 i += 1 for i in range(0, len(rating)): res += ( elm_less_than_cur_to_left[i] * elm_greater_than_cur_to_right[i] + elm_less_than_cur_to_right[i] * elm_greater_than_cur_to_left[i] ) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: def helper(team, rem): if len(team) == 3: self.count += 1 return for i in range(len(rem)): if len(team) == 1: helper(team + [rem[i]], rem[i:]) elif team[-2] < team[-1]: if rem[i] > team[-1]: helper(team + [rem[i]], rem[i:]) elif team[-2] > team[-1]: if rem[i] < team[-1]: helper(team + [rem[i]], rem[i:]) self.count = 0 for i in range(len(rating)): helper([rating[i]], rating[i:]) return self.count
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
from itertools import combinations class Solution: def numTeams(self, rating: List[int]) -> int: from itertools import combinations def satisfies_condition(inp): if inp[0] < inp[1] < inp[2] or inp[0] > inp[1] > inp[2]: return True team_count = 0 for x in list(combinations(rating, 3)): team_count += 1 if satisfies_condition(x) else 0 return team_count
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: length = len(rating) numberOfTeams = 0 for i in range(0, length): for j in range(0, length): if i < j: for k in range(0, length): if j < k: if rating[i] < rating[j] and rating[j] < rating[k]: numberOfTeams = numberOfTeams + 1 elif rating[i] > rating[j] and rating[j] > rating[k]: numberOfTeams = numberOfTeams + 1 return numberOfTeams
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def __init__(self): self.num_teams = 0 def numTeams(self, rating: List[int]) -> int: for i in range(len(rating)): first_soldier = rating[i] self.compareFirstSoldier(first_soldier, rating[i:]) reversed_rating = rating reversed_rating.reverse() for i in range(len(reversed_rating)): first_soldier = reversed_rating[i] self.compareFirstSoldier(first_soldier, reversed_rating[i:]) return self.num_teams def compareFirstSoldier(self, soldier_val: int, rating: List[int]) -> None: for j in range(len(rating)): if soldier_val < rating[j]: second_soldier = rating[j] self.compareSecondSoldier(second_soldier, rating[j:]) def compareSecondSoldier(self, soldier_val: int, rating: List[int]) -> None: for k in range(len(rating)): if soldier_val < rating[k]: self.num_teams += 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NONE FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER NONE
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def _is_valid_team(self, rating: List[int], i: int, j: int, k: int) -> bool: if rating[i] < rating[j] and rating[j] < rating[k]: return True if rating[i] > rating[j] and rating[j] > rating[k]: return True return False def numTeams(self, rating: List[int]) -> int: num_teams = 0 for i in range(len(rating)): for j in range(i + 1, len(rating)): for k in range(j + 1, len(rating)): if self._is_valid_team(rating, i, j, k): num_teams += 1 return num_teams
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: result = 0 for i in range(len(rating)): for j in range(i + 1, len(rating)): if rating[j] == rating[i]: continue if rating[j] < rating[i]: result += sum(num < rating[j] for num in rating[j + 1 :]) else: result += sum(num > rating[j] for num in rating[j + 1 :]) return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
from itertools import combinations class Solution: def numTeams(self, rating: List[int]) -> int: c = 0 k = list(combinations(rating, 3)) for i in k: if i[0] < i[1] and i[1] < i[2] or i[0] > i[1] and i[1] > i[2]: c += 1 return c
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def createAscGraph(self, ratings): graph = {} for index, rating in enumerate(ratings): graph[rating] = [] for rate in ratings[index + 1 :]: if rate > rating: graph[rating] += [rate] return graph def createDescGraph(self, ratings): graph = {} for index, rating in enumerate(ratings): graph[rating] = [] for rate in ratings[index + 1 :]: if rate < rating: graph[rating] += [rate] return graph def count_sequences(self, graph): counter = 0 for key in graph.keys(): for item in graph[key]: for item in graph[item]: counter += 1 return counter def numTeams(self, rating: List[int]) -> int: asc_graph = self.createAscGraph(rating) desc_graph = self.createDescGraph(rating) return self.count_sequences(asc_graph) + self.count_sequences(desc_graph)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR LIST VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR LIST VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FOR VAR VAR VAR FOR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: res = 0 l = [0] * len(rating) m = [0] * len(rating) for p, q in enumerate(rating): for j in range(p): if rating[j] < q: l[p] += 1 res += l[j] else: m[p] += 1 res += m[j] return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
from itertools import combinations class Solution: def numTeams(self, rating: List[int]) -> int: return len( [ (x, y, z) for x, y, z in list(combinations(rating, 3)) if x < y and y < z or x > y and y > z ] )
CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: count = 0 for i in range(len(rating)): for j in range(i + 1, len(rating)): for k in range(j + 1, len(rating)): if self.check(rating[i], rating[j], rating[k]): count += 1 return count def check(self, i, j, k): if i < j and j < k: return True elif i > j and j > k: return True else: return False
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: if len(rating) <= 2: return 0 res = 0 for i in range(len(rating) - 2): incs = [] decs = [] for j in range(i + 1, len(rating) - 1): if rating[j] > rating[i]: incs.append(j) elif rating[j] < rating[i]: decs.append(j) for ind in incs: for j in range(ind + 1, len(rating)): if rating[j] > rating[ind]: res += 1 for ind in decs: for j in range(ind + 1, len(rating)): if rating[j] < rating[ind]: res += 1 return res
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: result = 0 def dfs(i: int, cur_len: int, last_elem: int, flag: str): nonlocal result if cur_len == 3: result += 1 return for x in range(i, len(rating)): if flag == "l": if rating[x] > last_elem: dfs(x + 1, cur_len + 1, rating[x], "l") elif flag == "s": if rating[x] < last_elem: dfs(x + 1, cur_len + 1, rating[x], "s") for i in range(len(rating) - 1): dfs(i + 1, 1, rating[i], "l") dfs(i + 1, 1, rating[i], "s") return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FUNC_DEF VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR STRING IF VAR STRING IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR STRING RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: teams = range(1, len(rating) + 1) teams = list(zip(teams, rating)) teams_inc = sorted(teams, key=lambda x: x[1]) teams_dec = sorted(teams, key=lambda x: -x[1]) ans = 0 for i in range(0, len(teams) - 2): for j in range(i + 1, len(teams) - 1): for k in range(j + 1, len(teams)): if teams_inc[i][0] < teams_inc[j][0] < teams_inc[k][0]: ans += 1 for i in range(0, len(teams) - 2): for j in range(i + 1, len(teams) - 1): for k in range(j + 1, len(teams)): if teams_dec[i][0] < teams_dec[j][0] < teams_dec[k][0]: ans += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: return len(Solution.generate_teams(rating)) @staticmethod def generate_teams(rating): teams = [] rating_copy = rating negative_rating = [(-num) for num in rating] for rating in (rating_copy, negative_rating): for i in range(len(rating)): curr_first_soldier = rating[i] for j in range(i + 1, len(rating)): curr_second_soldier = rating[j] if i != j and curr_second_soldier > curr_first_soldier: for k in range(j + 1, len(rating)): curr_third_soldier = rating[k] if k != j and curr_third_soldier > curr_second_soldier: teams.append( ( curr_first_soldier, curr_second_soldier, curr_third_soldier, ) ) return teams class SolutionTest: @staticmethod def test_generate_teams(): test_ratings = [2, 3, 7, 1, 8, 9] actual_teams = [ (2, 3, 7), (2, 3, 8), (2, 3, 9), (2, 7, 8), (2, 7, 9), (2, 8, 9), (3, 7, 8), (3, 7, 9), (3, 8, 9), (7, 8, 9), (1, 8, 9), ] expected_teams = Solution.generate_teams(test_ratings) return actual_teams == expected_teams print(SolutionTest.test_generate_teams())
CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: count = 0 length = len(rating) if length < 3: return 0 for i in range(length): for j in range(i + 1, length): for k in range(j + 1, length): if ( rating[i] > rating[j] > rating[k] or rating[i] < rating[j] < rating[k] ): count += 1 return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
from itertools import combinations class Solution: def numTeams(self, rating: List[int]) -> int: if len(rating) in rating and len(rating) == 3: return 0 else: output = list(combinations(rating, 3)) count = len(output) for i in output: if i[0] < i[1] and i[1] > i[2]: count -= 1 elif i[0] > i[1] and i[1] < i[2]: count -= 1 return count
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: def combination(rating, tmp, index, ans, target): if len(tmp) == target: ans.append(tmp[:]) return for i in range(index, len(rating)): if len(tmp) == 2 and not ( tmp[0] > tmp[1] > rating[i] or rating[i] > tmp[1] > tmp[0] ): continue tmp.append(rating[i]) combination(rating, tmp, i + 1, ans, target) tmp.pop() possibles = [] combination(rating, [], 0, possibles, 3) count = 0 for a, b, c in possibles: if a < b < c or a > b > c: count += 1 return count
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
from itertools import combinations class Solution: def numTeams(self, rating: List[int]) -> int: indices = list(range(0, len(rating))) all_combs = combinations(indices, 3) result = 0 for comb in all_combs: if comb[0] < comb[1] and comb[1] < comb[2]: if ( rating[comb[0]] < rating[comb[1]] and rating[comb[1]] < rating[comb[2]] ): result += 1 elif ( rating[comb[0]] > rating[comb[1]] and rating[comb[1]] > rating[comb[2]] ): result += 1 return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: def checkLess(a, b, c): return rating[a] < rating[b] < rating[c] def checkG(a, b, c): return rating[a] > rating[b] > rating[c] if len(rating) < 3: return 0 if len(rating) == 3: if checkLess(0, 1, 2) or checkG(0, 1, 2): return 1 return 0 result = 0 n = len(rating) for i in range(n): for j in range(i, n): for k in range(j, n): if checkLess(i, j, k) or checkG(i, j, k): result += 1 return result
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: def getTeams(k, l, prev=None, asc=True): if k > len(l): return 0 if (prev is None) & (k == 1): return len(l) if k == 1: if asc: teamCount = sum([(x > prev) for x in l]) else: teamCount = sum([(x < prev) for x in l]) else: teamCount = 0 larger_x = True if prev is not None: larger_x = l[0] > prev if (larger_x == asc) | (prev is None): teamCount += getTeams(k - 1, l[1:], l[0], asc) teamCount += getTeams(k, l[1:], prev, asc) if 0: if asc: if larger_x | (prev is None): teamCount += getTeams(k - 1, l[1:], l[0], asc) teamCount += getTeams(k, l[1:], prev, asc) else: if larger_x == False | (prev is None): teamCount += getTeams(k - 1, l[1:], l[0], asc) teamCount += getTeams(k, l[1:], prev, asc) return teamCount return getTeams(3, rating, None, True) + getTeams(3, rating, None, False)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF NONE NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF BIN_OP VAR NONE VAR NUMBER RETURN FUNC_CALL VAR VAR IF VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NONE ASSIGN VAR VAR NUMBER VAR IF BIN_OP VAR VAR VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF NUMBER IF VAR IF BIN_OP VAR VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR BIN_OP NUMBER VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR NUMBER VAR NONE NUMBER FUNC_CALL VAR NUMBER VAR NONE NUMBER VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def canFormATeam(self, r1: int, r2: int, r3: int) -> bool: return r1 < r2 < r3 or r1 > r2 > r3 def numTeams(self, rating: List[int]) -> int: n_teams: int = 0 for i in range(len(rating) - 2): for j in range(i + 1, len(rating) - 1): for k in range(j + 1, len(rating)): if self.canFormATeam(rating[i], rating[j], rating[k]): n_teams += 1 return n_teams
CLASS_DEF FUNC_DEF VAR VAR VAR RETURN VAR VAR VAR VAR VAR VAR VAR FUNC_DEF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: track = [] res = 0 def backtrack(increase, index): nonlocal res if len(track) == 3: res += 1 return for i in range(index, len(rating)): if index == 0: track.append(rating[i]) backtrack(increase, i + 1) track.pop() else: if increase: if rating[i] > track[-1]: track.append(rating[i]) backtrack(True, i + 1) track.pop() if not increase: if rating[i] < track[-1]: track.append(rating[i]) backtrack(False, i + 1) track.pop() backtrack(True, 0) backtrack(False, 0) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: if not rating: return 0 asc, desc = 0, 0 for index in range(len(rating)): lrd, lri, rld, rli = 0, 0, 0, 0 for num in rating[:index]: if num < rating[index]: lri += 1 elif num > rating[index]: lrd += 1 for num in rating[index:]: if num > rating[index]: rli += 1 elif num < rating[index]: rld += 1 asc += lri * rli desc += lrd * rld return asc + desc
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: self.res = 0 for i in range(len(rating)): self.dfs_increase([rating[i]], rating[i + 1 :], 1) self.dfs_decrease([rating[i]], rating[i + 1 :], 1) return self.res def dfs_increase(self, stack, rating, count): if len(stack) == 3: self.res += 1 return for i in range(len(rating)): if rating[i] > stack[-1]: self.dfs_increase(stack + [rating[i]], rating[i + 1 :], count + 1) def dfs_decrease(self, stack, rating, count): if len(stack) == 3: self.res += 1 return for i in range(len(rating)): if rating[i] < stack[-1]: self.dfs_decrease(stack + [rating[i]], rating[i + 1 :], count + 1)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: n = len(rating) res = 0 for p in range(n - 2): for q in range(p + 1, n - 1): if rating[p] > rating[q]: for o in range(q + 1, n): if rating[q] > rating[o]: res += 1 for p in range(n - 2): for q in range(p + 1, n - 1): if rating[p] < rating[q]: for o in range(q + 1, n): if rating[q] < rating[o]: res += 1 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: n = len(rating) def get_triplets(r): res = 0 for i in range(n): for j in range(i + 1, n): for k in range(j + 1, n): res += 1 if r[i] < r[j] < r[k] else 0 return res return get_triplets(rating) + get_triplets(rating[::-1])
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: possible_list = [] count = 0 for i, first_num in enumerate(rating[:-2]): for j, sec_num in enumerate(rating[i + 1 : -1]): for k, third_num in enumerate(rating[j + i + 2 :]): possible_list.append((first_num, sec_num, third_num)) for pair in possible_list: if pair[0] > pair[1] > pair[2] or pair[2] > pair[1] > pair[0]: count += 1 return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: n = len(rating) total = 0 for i in range(n): for j in range(i, n): for k in range(j, n): total += any( [ rating[i] < rating[j] < rating[k], rating[i] > rating[j] > rating[k], ] ) return total
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: m_counter = 0 for index, v1 in enumerate(rating): for jdex, v2 in enumerate(rating[index + 1 :]): if v1 < v2: m_counter = m_counter + len( [k for k in rating[jdex + index + 1 :] if k > v2] ) else: m_counter = m_counter + len( [k for k in rating[jdex + index + 1 :] if k < v2] ) print(m_counter) return m_counter
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: list) -> int: left_less, left_greater = 0, 0 right_less, right_greater = 0, 0 count = 0 for j in range(len(rating)): for i in range(j): if rating[i] < rating[j]: left_less += 1 elif rating[i] > rating[j]: left_greater += 1 for k in range(j + 1, len(rating)): if rating[j] < rating[k]: right_greater += 1 elif rating[j] > rating[k]: right_less += 1 count += left_less * right_greater + left_greater * right_less left_less = left_greater = right_less = right_greater = 0 return count
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: max_rating = 100000 def strictly_monotonic( rating: List[int], previous_rating: int, remaining_ratings: int, ascending: bool, ) -> int: if not remaining_ratings: return 1 if not rating: return 0 if ascending ^ (rating[0] < previous_rating): return strictly_monotonic( rating[1:], rating[0], remaining_ratings - 1, ascending ) + strictly_monotonic( rating[1:], previous_rating, remaining_ratings, ascending ) else: return strictly_monotonic( rating[1:], previous_rating, remaining_ratings, ascending ) return strictly_monotonic(rating, 0, 3, ascending=True) + strictly_monotonic( rating, max_rating + 1, 3, ascending=False )
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FUNC_DEF VAR VAR VAR VAR VAR IF VAR RETURN NUMBER IF VAR RETURN NUMBER IF BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: if len(rating) < 3: return 0 teams = set() for i in range(0, len(rating) - 2): for j in range(i + 1, len(rating) - 1): for k in range(j + 1, len(rating)): if rating[i] < rating[j] < rating[k]: teams.add((i, j, k)) if rating[i] > rating[j] > rating[k]: teams.add((i, j, k)) return len(teams)
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: def ascend(nums, curLargest, count): if count == 3: return 1 if not nums: return 0 total = 0 for i in range(len(nums)): if nums[i] > curLargest: total += ascend(nums[i + 1 :], nums[i], count + 1) return total def descend(nums, curSmallest, count): if count == 3: return 1 if not nums: return 0 total = 0 for i in range(len(nums)): if nums[i] < curSmallest: total += descend(nums[i + 1 :], nums[i], count + 1) return total return ascend(rating, 0, 0) + descend(rating, float("inf"), 0)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: if len(rating) < 3: return 0 teams: int = 0 for i, s1 in enumerate(rating): for j in range(i, len(rating)): for k in range(j, len(rating)): s2 = rating[j] s3 = rating[k] if s1 > s2 > s3 or s1 < s2 < s3: teams += 1 return teams
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: count = 0 for i, r_i in enumerate(rating): for j, r_j in enumerate(rating[i + 1 :], i + 1): if r_i == r_j: continue if r_i < r_j: for k, r_k in enumerate(rating[j + 1 :], j + 1): count += r_j < r_k else: for k, r_k in enumerate(rating[j + 1 :], j + 1): count += r_j > r_k return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR
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).   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 1 <= n <= 200 1 <= rating[i] <= 10^5
class Solution: def numTeams(self, rating: List[int]) -> int: res = 0 record = [] rec = [] for i in range(0, len(rating)): record.append([]) for j in range(i + 1): record[i].append(None) for j in range(i + 1, len(rating)): if rating[j] > rating[i]: record[i].append(True) elif rating[j] < rating[i]: record[i].append(False) for i in range(0, len(rating) - 1): for j in range(i + 1, len(rating)): if record[i][j]: for k in range(j + 1, len(rating)): if record[j][k]: res += 1 rec.append((rating[i], rating[j], rating[k])) elif record[i][j] == False: for k in range(j + 1, len(rating)): if record[j][k] == False: res += 1 rec.append((rating[i], rating[j], rating[k])) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NONE FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR