1. Code
'''
Check one by one from the back of the list.
Think of the case in which the number in front (ith) < the number in the back (jth) or 'ith' >= 'jth'.
If 'ith' is greater than 'jth', add 1 to list up[i].
Otherwise, add 1 to list down[i].
Add the number up/down [j] to 'result'.
This is to find something like this situation in the back when it is ith<jth or ith>=jth.
'''
class Solution:
def numTeams(self, rating: List[int]) -> int:
n = len(rating)
up = [0] * n
down = [0] * n
result = 0
for i in range(n-2, -1, -1):
for j in range(i+1, n):
if rating[i] < rating[j]:
up[i] += 1
result += up[j]
else:
down[i] += 1
result += down[j]
return result
2. Result
Runtime : 56 ms(94.55%), Memory usage : 14.2 MB(73.62%)
(Runtime can be different by a system even if it is a same code.)