[Python] Easy to understand  O(N^2) Dynamic Programming and O(N^3)
O(N^2) DP\n\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        if n < 3:\n            return 0\n        \n   
**O(N^2) DP**\n```\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        if n < 3:\n            return 0\n        \n        g = [0] * n # <- Greater count\n        s = [0] * n # <- Smaller count\n        res = 0 # <- number of teams\n        \n        # For each soldier, we search the next greater and smaller\n        for i in range(n - 1):\n            for j in range(i + 1, n):\n                if rating[j] > rating[i]: \n                    g[i] += 1\n                else:\n                    s[i] += 1\n        \n        # Now, we looking for the "last number" . If rating[i] < rating[j] then g[j] are the following possible numbers\n        #                                       .    rating[i] > rating[j]      s[j] \n        for i in range(n - 2):\n            for j in range(i + 1, n):\n                if rating[j] > rating[i]:\n                    res += g[j]\n                else:\n                    res += s[j]\n        \n        return res\n```\n\n**O(N^3)**\n```\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        mx = max(rating)\n        mn = min(rating)\n        n = len(rating)\n        n1 = n - 1\n        \n        res = 0\n        \n        for i in range(n - 2):\n            for j in range(i + 1, n1):\n                if rating[i] < rating[j] and rating[j] == mx:\n                    continue\n                if rating[i] > rating[j] and rating[j] == mn:\n                    continue\n                \n                for k in range(j + 1, n):\n                    if rating[i] > rating[j] > rating[k] or rating[i] < rating[j] < rating[k]: \n                        res += 1\n        return res\n```
['Dynamic Programming', 'Python3']