题解 | #数组中的逆序对#
数组中的逆序对
https://www.nowcoder.com/practice/96bd6684e04a44eb80e6a68efc0ec6c5
from bisect import bisect_right class Solution: def InversePairs(self, nums: List[int]) -> int: dp = [] ans = 0 for num in nums: # 使用二分查找找到插入点 pos = bisect_right(dp, num) ans += len(dp) - pos # 大于当前数字的元素数量 dp.insert(pos, num) # 在有序数组中插入当前数字 return ans%1000000007
我爱python