首页 > 其他分享 >leetcode -- tree 3

leetcode -- tree 3

时间:2022-09-30 19:02:20浏览次数:44  
标签:mergesort nums -- List self tree mid int leetcode

使用归并排序简单解决问题

  1. 归并排序
用传统方法归并
class Solution:
    def sortArray(self, nums: List[int]) -> List[int]:
        def mergesort(nums: List[int], low: int, high: int) -> None:
            if low == high:
                return

            mid = (high + low)>>1
            mergesort(nums, low, mid)
            mergesort(nums, mid + 1, high)

            tmp = []
            l, h = low, mid + 1
            
            while l <= mid or h <= high:
                if l > mid or (h <= high and nums[h] < nums[l]):
                    tmp.append(nums[h])
                    h += 1
                else:
                    tmp.append(nums[l])
                    l += 1

            nums[low : high + 1] = tmp

        
        mergesort(nums, 0, len(nums) - 1)
        return nums
        
        
  1. 区间和的个数
前缀和 + 归并排序
class Solution:
    # prewindow 可以用 sortedlist 替代
    def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:
        # 创建前缀和数组
        presum = [0 for i in range(len(nums) + 1)]
        for i in range(len(nums)):
            presum[i + 1] = nums[i] + presum[i]
        
        res = 0
        prewindow = []
        for _, p in enumerate(presum):
            L = bisect_left(prewindow, p - upper)
            R = bisect_right(prewindow, p - lower)
            res += R - L
            bisect.insort(prewindow, p)
        
        return res

# 归并排序中进行嵌套处理
class Solution:
    def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:
        self.lower = lower 
        self.upper = upper
        self.res = 0 
        
        # create the presum
        n = len(nums)
        presum = [0 for _ in range(n + 1)]
        for i in range(n):
            presum[i + 1] = presum[i] + nums[i]
        
        self.mergesort(presum, 0, n)
        return self.res

    def mergesort(self, nums: List[int], L: int, R: int) -> None:
        if L < R:
            mid = L + (R - L) // 2
            self.mergesort(nums, L, mid)
            self.mergesort(nums, mid + 1, R)
            self.merge(nums, L, mid, R)


    def merge(self, nums:List[int], low: int, mid: int, high: int) -> None:
        tmp = []
        i, j = low, mid + 1
        while i <= mid or j <= high:
            if i > mid or (j <= high and nums[j] < nums[i]):
                tmp.append(nums[j])
                j += 1
            else:
                tmp.append(nums[i])
                i += 1
        
        i, l, r = low, mid + 1, mid + 1
        while i <= mid:
            while l <= high and nums[l] < nums[i] + self.lower:
                l += 1
            while r <= high and nums[r] <= nums[i] + self.upper:
                r += 1
            self.res += r-l
            i += 1

        # for _ in range(mid - low + 1):
        #     l = bisect_left(nums, nums[i] + self.lower, lo=l, hi=high)
        #     r = bisect_right(nums, nums[i] + self.upper, lo=r, hi=high)
        #     self.res += r-l
        #     i += 1
        

        nums[low: high + 1] = tmp
  1. 翻转对
归并排序
class Solution:
    def reversePairs(self, nums: List[int]) -> int:
        self.res = 0
        self.mergesort(nums, 0, len(nums) - 1)

        return self.res

    
    def mergesort(self, nums: List[int], L: int, R: int) -> None:
        if L < R:
            mid = L + (R - L) // 2
            self.mergesort(nums, L, mid)
            self.mergesort(nums, mid + 1, R)
            self.merge(nums, L, mid, R)



    def merge(self, nums: List[int], L: int, M: int, R: int) -> None:
        tmp = []
        i, j = L, M + 1
        while i <= M or j <= R:
            if i > M or (j <= R and nums[j] < nums[i]):
                tmp.append(nums[j])
                j += 1
            else:
                tmp.append(nums[i])
                i += 1

        i, j = L, M + 1
        while i <= M:
            while j <= R and nums[i] > 2 * nums[j]:
                j += 1
            self.res += j - M - 1
            j = M + 1
            i += 1
        
        nums[L: R + 1] = tmp

标签:mergesort,nums,--,List,self,tree,mid,int,leetcode
From: https://www.cnblogs.com/DocGu/p/16745860.html

相关文章

  • 2-variable Function
    ProblemStatementGivenaninteger$N$,findthesmallestinteger$X$thatsatisfiesalloftheconditionsbelow.$X$isgreaterthanorequalto$N$.Therei......
  • ASP.NET Core – Dependency Injection
    前言很久很久以前就写过了Asp.netcore学习笔记(DI依赖注入),这篇只是整理一下而已. 参考Usingdependencyinjectionina.NetCoreconsoleapplication ......
  • 测试4错误纠正
    没错!还是那神奇的四则运算,它,,又来啦!!!话不多说,直接上代码!!!本次实践,主要实现了分年级的继承功能问题、错题集整理和重做问题、查看正确率问题、还有之前已经实现过的四则运算......
  • 报告分享|2022年中国数字化办公市场研究报告
     报告链接:http://tecdat.cn/?p=28674数字化办公作为现代化办公的全新模式,其发展可追溯至19世纪末期自泰勒提出了科学式管理理论,进一步的发展归因于相关软硬件技术的创新......
  • Android Studio 制作微信界面
    最近学习了安卓开发的相关知识,下面通过实践练习来巩固学到的知识。设计的目标是完成微信的门户页面框架设计。最终要实现的效果如下:在页面的顶端,是微信的名字“Wechat”......
  • 全网最强资源搜索站之一(绝对高质量)
    1、找资源2、千帆搜索3、奈斯搜索4、telegram资源搜索5、人人资源视频站......
  • 报告分享|数字化转型,从战略到执行报告
    报告链接:http://tecdat.cn/?p=28672如何加速国家、城市、行业、企业数字化进程,激发数字经济新动能。这份报告通过洞察数字化的6大改变、4大载体、4个阶段、20+场景、100+......
  • 报告分享|2022年企业数字化人才发展白皮书
    报告链接:http://tecdat.cn/?p=28670数字经济时代,企业对数字化人才的需求急剧增长。此报告对数字化人才培养和企业数字化人才发展现状进行梳理和研究,聚焦于金融、零售、能......
  • Coupon
    ProblemStatementThereare$N$itemsinashop.Foreach$i=1,2,\ldots,N$,thepriceofthe$i$-thitemis$A_i$yen(thecurrencyofJapan).Takahashih......
  • 1. JavaScript--简介
    1.前言JavaScript(简称“JS”)是当前最流行、应用最广泛的客户端脚本语言,用来在网页中添加一些动态效果与交互功能,在Web开发领域有着举足轻重的地位。JavaScript与HTML......