Python O(n^2) sol by sliding range [w/ Diagram] 有中文解析文章
\u4E2D\u6587\u89E3\u6790\u6587\u7AE0\n\nPython O(n^2) sol by sliding range\n\n---\n\nDiagram and abstract model:\n\n\n\n\n\n---\n\nImplementation:\n\n\nclass So
[\u4E2D\u6587\u89E3\u6790\u6587\u7AE0](https://leetcode.com/problems/count-number-of-teams/solutions/555469/python-o-n-2-sol-by-sliding-range-w-diagram/?envType=daily-question&envId=2024-07-29)\n\nPython O(n^2) sol by sliding range\n\n---\n\n**Diagram** and **abstract model**:\n\n![image](https://assets.leetcode.com/users/brianchiang_tw/image_1585491916.png)\n\n\n\n---\n\n**Implementation**:\n\n```\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        r, size = rating, len( rating )\n        \n\t\t# compute statistics of sliding range\n        left_smaller = [ sum( r[i] < r[j] for i in range(0,j) ) for j in range(size) ]\n        right_bigger = [ sum( r[j] < r[k] for k in range(j+1, size) ) for j in range(size)]\n        \n        num_of_teams = 0\n\t\t\n\t\t# j slides from 0 to ( n-1 ), and j stands for the index of middle element\n        for j in range( 0, size):\n\t\t\n            num_of_ascending_team = left_smaller[j] * right_bigger[j]\n\t\t\t\n            num_of_descending_team = ( j - left_smaller[j] ) * ( size-1 - j - right_bigger[j] )\n            \n            num_of_teams += ( num_of_ascending_team + num_of_descending_team )\n            \n        return num_of_teams\n```\n\n---\n\n**Implementation**, rewritten in generator expression:\n\n```\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        r, size = rating, len( rating )\n        \n\t\t# compute statistics of sliding range\n        left_smaller = [ sum( r[i] < r[j] for i in range(0,j) ) for j in range(size) ]\n        right_bigger = [ sum( r[j] < r[k] for k in range(j+1, size) ) for j in range(size)]\n        \n        \n        return sum( left_smaller[j] * right_bigger[j] + ( j - left_smaller[j] ) * ( size-1 - j - right_bigger[j] ) for j in range( 0, size)  )\n```\n\n---\n\nReference:\n\n[1] [Python official docs about generator expression](https://www.python.org/dev/peps/pep-0289/)
['Array', 'Dynamic Programming', 'Sliding Window', 'Python', 'Python3']