题解 | #右侧更小数#

右侧更小数

http://www.nowcoder.com/practice/b47363e689bc4a8088a68631d0c2754d

快排思想超时,用归并思想可以解决问题 和smallerSum没多大区别,思想还是传递已有信息

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param nums int整型一维数组 
# @return int整型一维数组
#
class Solution:
    def realSort(self,arr,s,mid,e,count):
        t1=arr[s:mid+1]
        t2=arr[mid+1:e+1]
        temp=[]
        p1=s
        p2=mid+1
        while p1<=mid and p2<=e:
            if arr[p1][0]>arr[p2][0]:
                temp.append(arr[p1])
                count[arr[p1][1]]+=(e-p2+1)
                p1 += 1
            else:
                temp.append(arr[p2])
                p2 += 1

        temp+=arr[p1:mid+1]
        temp += arr[p2:e+1]
        if len(temp)==len(arr[s:e+1]):
            arr[s:e+1]=temp
        else:
            print('error')
    def fakeSort(self,arr,s,e,count):
            if (e-s)<1:
                return
            mid=s+(e-s)//2
            self.fakeSort(arr,s,mid,count)
            self.fakeSort(arr,mid+1,e,count)
            self.realSort(arr,s,mid,e,count)
    def smallerCount(self , nums: List[int]) -> List[int]:
            count = [0] * len(nums)
            if len(nums) < 2:
                return count
            nums=[(i,v) for v,i in enumerate(nums)]
            self.fakeSort(nums,0,len(nums)-1,count)
            return count
        # write code here
全部评论

相关推荐

有担当的灰太狼又在摸鱼:零帧起手查看图片
点赞 评论 收藏
分享
爱睡觉的冰箱哥:学历不够啊兄弟,分析师都是9硕✌🏻咱搞不过他们滴
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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