【Leetcode】327. Count of Range Sum 327. 区间和的个数

使用BK3432芯片进行蓝牙开发的详细指南:编程手册与实际应用 BK3432是一款蓝牙4.2双模式芯片,支持蓝牙低功耗(BLE)和经典蓝牙(BR/EDR)模式。它集成了高性能的RF收发器、基带处理器、ARM9E内核、丰富的功能外设单元,并支持BLE应用的可编程协议和概要文件。蓝牙版本:4.2数据速率:最高支持2Mbps内核:ARM9ERF收发器:高性能、低功耗外设单元:包括UART、SPI、I2C、GPIO等功耗:超低功耗设计,适合电池供电的应用。 阅读详情

1

解法

naive的方法是O(n2)O(n^2)O(n2)的
要找更高效的方法,都离不开先计算出一个前缀和数组p,然后对于每个元素p[i],要计算i以后有多少个元素在[p[i]+lower,p[i]+upper]范围内

解法一:归并排序

在merge的过程中会得到两个有序的数组L,R
对于L里的每个元素a,需要在R里找到[a+lower,a+upper]范围里的元素的个数,由于R是有序的,所以用尺取法就可以在O(n)O(n)O(n)时间内完成,所以最后复杂度为O(nlogn)O(nlogn)O(nlogn)

class Solution(object):
    def countRangeSum(self, nums, lower, upper):
        """
        :type nums: List[int]
        :type lower: int
        :type upper: int
        :rtype: int
        """
        from bisect import bisect_left,bisect_right,insort
        if not nums or upper<lower:
            return 0
        p = [0]
        for a in nums:
            p.append(p[-1]+a)
        n = len(p)
        self.ans = 0
        def mergesort(A):
            if len(A)<=1:
                return A
            mid = len(A)>>1
            B,C = mergesort(A[:mid]),mergesort(A[mid:])
            n = len(C)
            l = r = 0
            for b in B:
                lo,hi = b+lower,b+upper
                while l<n and C[l]<lo:
                    l += 1
                while r<n and C[r]<=hi:
                    r += 1
                self.ans += r-l
            res = []
            i = j = 0
            while i<mid:
                while j<n and C[j]<B[i]:
                    res.append(C[j])
                    j += 1
                res.append(B[i])
                i += 1
            while j<n:
                res.append(C[j])
                j += 1
            return res
        mergesort(p)
        return self.ans

解法二:TreeMap

倒序遍历前缀和数组,当遍历到p[i]的时候,它往后的元素都已经遍历过,它们都插入了一个有序的TreeMap,查找两个边界需要O(logn)O(logn)O(logn),然后再插入p[i]需要O(logn)O(logn)O(logn),问复杂度为O(nlogn)O(nlogn)O(nlogn)
由于python没有TreeMap的结构,所以用bisect来代替

class Solution(object):
    def countRangeSum(self, nums, lower, upper):
        """
        :type nums: List[int]
        :type lower: int
        :type upper: int
        :rtype: int
        """
        from bisect import bisect_left,bisect_right,insort
        if not nums or upper<lower:
            return 0
        p = [0]
        for a in nums:
            p.append(p[-1]+a)
        walked = []
        ans = 0
        for a in p[::-1]:
            l,r = a+lower,a+upper
            i,j = bisect_left(walked,l),bisect_right(walked,r)
            ans += j-i
            insort(walked,a)
            # print walked
        return ans

解法三:离散化树状数组

这种统计在某个区间内的数字的个数,也可以用树状数组来解决
由于数字的范围可能很大,所以要离散化,涉及到的需要被离散化的值有p[i],p[i]+lower,p[i]+upper。
树状数组的查询是O(logn)O(logn)O(logn)的,所以最后复杂度为O(nlogn)O(nlogn)O(nlogn)

class Solution(object):
    def countRangeSum(self, nums, lower, upper):
        """
        :type nums: List[int]
        :type lower: int
        :type upper: int
        :rtype: int
        """
        if not nums or upper<lower:
            return 0
        p = [0]
        for a in nums:
            p.append(p[-1]+a)
        value = set()
        for a in p:
            value |= {a,a+lower,a+upper}
        value = sorted(value)
        idx={}
        for i,a in enumerate(value):
            idx[a]=i
        n = len(value)
        c = [0]*n
        def lowbit(k):
            return k&-k
        def add(k):
            k +=1
            while k<=n:
                c[k-1]+=1
                k+=lowbit(k)
        def tsum(k):
            res = 0
            k+=1
            while k>=1:
                res += c[k-1]
                k -= lowbit(k)
            return res
        ans = 0
        for a in p[::-1]:
            l,r = idx[a+lower],idx[a+upper]
            ans += tsum(r)
            if l>0:
                ans -= tsum(l-1)
            add(idx[a])
        return ans

emmm……最后最快的是方法二

Cloudflare 5秒盾破解实战:Python补环境框架下的13次请求全解析 本文深入解析Cloudflare 5秒盾(5-second challenge)的工作原理,并提供基于Python环境的自动化应对策略。通过13次请求流程分解、动态脚本处理技术和环境指纹模拟,帮助开发者有效绕过这一复杂的人机验证机制,提升自动化工作效率。 阅读详情

相关推荐

【论文阅读】LIME:Low-light Image Enhancement via Illumination Map Estimation(笔记最全篇)

文章目录AbstractIntroductionContributionMethodSpeed-up Method(1):ALM子问题 T子问题 G子问题 Z和μ\muμSpeed-up Method(2):权重变量实验conclusion 今天要整理的一篇论文是使用传统的方法解决低照度的问题,Guo等人于2016年发表在IEEE上,论文《LIME:Low-light Image Enhance...

florrie 2万+

327. Count of Range Sum

327. Count of Range Sum方法1:presum + map易错点:Complexity方法2:merge sort Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive. Range sum S(i, j) is defined as t...

wilzxu的博客 326

别再死记硬背了!用TwinCAT 3手把手教你配置EtherCAT伺服驱动的PDO映射(DS402协议)

本文详细介绍了如何使用TwinCAT 3配置EtherCAT伺服驱动的PDO映射(DS402协议),涵盖环境准备、设备扫描、PDO映射实战及高级技巧。通过具体操作步骤和常见问题排查,帮助工程师快速掌握EtherCAT通信配置,提升运动控制系统的效率和稳定性。

weixin_29185493的博客 332

LeetCode每日一题(327. Count of Range Sum)

我们的目标是从 nums 里找到符合条件的切片, 我们将 nums 平分成两半, left = nums[…m], right = nums[m…=j]符合条件, 那 i 和 j 就三种情况, 要么都在 left, 要么都在 right, 要么 i 在 left, j 在 right。还有一点就是,我们在计算 i 在 left,j 在 right 的情况的时候, 我们用到了 prefix sum, 比较特别的是我们在生成 prefix sum 之后对它进行了排序, 之所以这样做是为了可以用二分法进行搜索。

wangjun861205的博客 443

EBOM转MBOM实战:华为云数字工厂如何5步搞定BOM自动转换(含避坑指南)

本文详细解析了华为云数字工厂平台如何通过五步自动化流程实现EBOM到MBOM的高效转换,涵盖模型预配置、逻辑流编排、工艺属性注入等关键步骤,并提供了多工厂差异化配置、历史数据迁移等典型场景的避坑指南。该方案显著提升转换效率,降低差错率,助力企业实现数字化转型。

weixin_29215391的博客 275

论文代码复现 | 无人机与卡车联合配送(Python+Gurobi)(The flying sidekick traveling salesman problem)

无人机配送-代码复现The flying sidekick traveling salesman problem-Part 1文献笔记:(无人机配送-代码复现)The flying sidekick traveling salesman problem-Part 1摘要论文主要图标摘录数值实验算例结果参考文献 文献笔记:(无人机配送-代码复现)The flying sidekick traveling salesman problem-Part 1 摘要 Once limited to the milit

HsinglukLiu的博客 3万+

LeetCode: 327. Count of Range Sum

LeetCode: 327. Count of Range Sum Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive. Range sum S(i, j) is defined as the sum of the elements in nums bet...

杨领well的专栏 314

Cocos Creator 3D音频定位实战:从原理到沉浸感实现

3D音频定位是游戏开发中实现沉浸感的关键技术,其核心原理基于HRTF(头部相关传输函数)模型,模拟声音在空间中的传播与双耳感知差异。这项技术通过赋予声音空间坐标、距离衰减和方向属性,将音频从背景元素提升为可交互的环境线索,极大增强了虚拟世界的真实感与玩家的空间认知。在工程实践中,开发者需要深入理解声源、听者、衰减曲线与多普勒效应等基础概念,并合理运用音频池、优先级管理与动态混音等技术手段来平衡效果与性能。对于Cocos Creator开发者而言,无论是使用引擎原生的AudioSource组件构建轻量级方案,

weixin_30972263的博客 301

LeetCode 327 Count of Range Sum(分治)

题意:给出一个序列,求出区间和在[lower, upper]之间的区间数量。 思路:分治。(如果数据特殊一点可以用树状数组) 首先预处理出前缀和sum[i],可以发现任何一个区间[i,j]的和都可以表示成sum[j]-sum[i-1], 所以现在问题转化为了求有多少对i,j(j>I),使得sum[j]-sum[i-1]在lower与upper之间。 这个可以用分治来做,对于一个左闭右开的区

GODSPEED 1419

新手避坑:RT-Thread-Studio外设驱动配置最容易忽略的3个地方(附WDT实例)

本文针对RT-Thread-Studio新手在配置外设驱动时常见的编译报错问题,以看门狗(WDT)为例,详细解析了最易忽略的三个关键配置环节:RT-Thread-Studio的图形化Setting开关、Kconfig中的依赖关系以及SConscript的编译规则。掌握这三层配置模型,能有效避免头文件包含正确却依然编译失败的问题,确保驱动代码被正确加入构建。

gray5的博客 509

7、Python高光谱分析工具(PyHAT)简介

Python高光谱分析工具(PyHAT)是一个开源、跨平台的Python库及图形用户界面,用于行星光谱数据的可视化与分析。它支持多种高光谱传感器数据,如火星勘测轨道飞行器的CRISM、月船1号的M3以及SELENE Kaguya SP点光谱仪,提供预处理、衍生产品生成、统计学习和机器学习分析功能。PyHAT基于Pandas和NumPy构建,兼容Jupyter和QGIS环境,旨在解决现有光谱分析方法缺乏开源实现、难以探索性分析和算法分散的问题。其模块化架构涵盖数据输入/输出、通用光谱变换、测绘与点观测工具及高

aa123的博客 147

[Swift]LeetCode327. 区间和的个数 | Count of Range Sum

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/streng...

weixin_30906185的博客 136

【部署】Ubuntu 24.04.4虚拟机下部署OpenClaw

OpenClaw是一款基于AI的智能助手工具,本文详细介绍了在Ubuntu 24.04.4系统上的三种安装方法。推荐使用一键安装脚本(curl -fsSL https://openclaw.ai/install.sh | bash),自动完成Node.js检测、CLI安装和引导配置。对于开发者,提供了从源码构建的详细步骤,包括环境准备、依赖安装和构建流程。文章还包含常见问题解决方案(如sharp构建错误处理)以及后续维护建议(更新、配置检查和日志查看)。安装完成后,用户可通过终端TUI或Web UI进行初始

海兰 1万+

【LeetCode】327. Count of Range Sum 区间和的个数(Hard)(JAVA)

【LeetCode】327. Count of Range Sum 区间和的个数(Hard)(JAVA) 题目地址: https://leetcode.com/problems/count-of-range-sum/ 题目描述: Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive. Range sum S(i, j) is defined as the sum of

qq_16927853的博客 250

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

【LeetCode】 327. Count of Range Sum 区间和的个数(Hard)(JAVA)每日一题

【LeetCode】 327. Count of Range Sum 区间和的个数(Hard)(JAVA) 题目地址: https://leetcode.com/problems/count-of-range-sum/ 题目描述: Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive. Range sum S(i, j) is defined as the sum o

qq_16927853的博客 210

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Leetcode每日一题:327.count-of-range-sum(区间和的个数)

思路:我还是用的暴力法,什么离散化+线段树等等,看不怎么懂;C++用暴力法直接超时,由于最近在学Go语言,索性用Go测试一波,结果震惊到了: func countRangeSum(nums []int, lower int, upper int) int { size := len(nums) if size == 0 { return 0 } res := 0 for i := 0; i < size; i++ { sum := 0 for j := i; j < s.

CodeLuweir 的博客 296

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

[leetcode] 327. Count of Range Sum 解题报告

题目链接: https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive. Range sum S(i, j) is defined as the s

小榕流光的专栏 8179

LeetCode 327. Count of Range Sum(区间和计数)

原题网址:https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive. Range sum S(i, j) is defined as the su

James Pan 3867

[Leetcode] 327. Count of Range Sum 解题报告

题目: Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive. Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j

魔豆(Magicbean)的博客 1135

LeetCode 327. Count of Range Sum(TreeMap)

Count of Range Sum Hard Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive. Range sum S(i, j) is defined as the sum of the elements in nums between indic...

da_kao_la的博客 278

【Leetcode】327. Count of Range Sum

题目地址: https://leetcode.com/problems/count-of-range-sum/ 给定一个数组AAA,再给定两个数lll和rrr,求AAA的和位于[x,y][x,y][x,y]的子区间个数。 先求AAA的前缀和数组ppp,即p[k]p[k]p[k]是AAA的前kkk个数的和。某个子区间的和是p[r]−p[l−1]p[r]-p[l-1]p[r]−p[l−1],要满足x≤p[r]−p[l−1]≤yx\le p[r]-p[l-1]\le yx≤p[r]−p[l−1]≤y,有:p[r]

数学、算法爱好者的博客 316

LeetCode 327. Count of Range Sum

对于count range sum的题目 首先应该想到的就是 树状数组和线段树就是用来高效解决此类问题的方法。 首先题意就费了一些力气去理解: Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive. Range sum S(i, j) is defined as the sum of the elements in nums between indices i and

Tech in Pieces 414

LeetCode //C - 327. Count of Range Sum

【代码】LeetCode //C - 327. Count of Range Sum。

Made in Code 1190

LeetCode-327 Count of Range Sum

题目描述 Given an integer arraynums, return the number of range sums that lie in[lower, upper]inclusive.Range sumS(i, j)is defined as the sum of the elements innumsbetween indicesiandj(i≤j),...

weixin_30810583的博客 88

leetcode 327. Count of Range Sum

/* leetcode 327. Count of Range SumGiven an integer array nums, return the number of range sums that lie in [lower, upper] inclusive. Range sum S(i, j) is defined as the sum of the elements in nums bet

编码之路 586

LeetCode 327.Count of Range Sum

Given an integer arraynums, return the number of range sums that lie in[lower, upper]inclusive.Range sumS(i, j)is defined as the sum of the elements innumsbetween indicesiandj(i≤j), i...

weixin_30881367的博客 130
上一篇: 【Leetcode】699. Falling Squares 699. 掉落的方块
下一篇: 【Leetcode】754. Reach a Number 754. 到达终点数字
MYSDB
博客等级 码龄9年 8粉丝 151原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值