题解 | #数组中的逆序对#

数组中的逆序对

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

全部评论

相关推荐

迷茫的大四🐶:💐孝子启动失败,改为启动咏鹅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务