归并排序面试题——区间和的个数

简介: 归并排序面试题——区间和的个数

题目

给你一个整数数组 nums 以及两个整数 lower 和 upper 。求数组中,值位于范围 [lower, upper] (包含 lower 和 upper)之内的区间和的个数 。区间和 S(i, j) 表示在 nums 中,位置从 i 到 j 的元素之和,包含 i 和 j (i ≤ j)。

力扣链接:区间和个数

前置知识

前缀和:

sum[i...j]=arr[0...j]-arr[0...i-1]

准备一个前缀和数组presum,长度和原数组长度一样。如下图:
在这里插入图片描述

问题转换

1.假设0~i位置整体累加和是X,求必须以i位置结尾的子数组,有多少个在[lower,upper],就等同于去求,i之前的所有前缀和中有多少前缀和在[X-upper,X-lower]上

2.原数组[lower,upper] —> 前缀和数组中的每一个X 求[X-lower,X-upper]

3 . 传进来的每一个前缀和数组 有多少个落在[X-lower,X-upper]

4 . 放在归并排序里面求

代码:

package com.harrison.class03;

/**
 * @author Harrison
 * @create 2022-02-24-22:01
 * @motto 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。
 */
public class Code05_CountOfRangeSum {
    public static int countRangeSum(int[] nums,int lower,int upper){
        if(nums==null || nums.length==0){
            return 0;
        }
        long[] sum=new long[nums.length];
        sum[0]=nums[0];
        for(int i=1; i<nums.length; i++){
            sum[i]=sum[i-1]+nums[i];
        }

        return process(sum,0,nums.length-1,lower,upper);
    }

    // arr[L...R]已经不传进来了,只传进来sum(前缀和数组!!!)
    // 求在原始数组arr[L...R]中,有多少个子数组累加和在[lower,upper]上
    public static int process(long[] sum, int L, int R, int lower, int upper){
        if(L==R){
            return sum[L]>=lower && sum[L]<=upper?1:0;
        }
        // L!=R 范围上不止一个位置
        int M=L+((R-L)>>1);
        return process(sum,L,M,lower,upper)+
                process(sum,M+1,R,lower,upper)+
                merge(sum,L,M,R,lower,upper);
    }

    public static int merge(long[] sum,int L,int M,int R,int lower,int upper){
        int ans=0;
        int windowL=L;
        int windowR=L;
        // [windowL,windowR)
        for(int i=M+1; i<=R; i++){
            long min=sum[i]-upper;
            long max=sum[i]-lower;
            while(windowR<=M && sum[windowR]<=max){
                windowR++;
            }
            while(windowL<=M && sum[windowL]<min){
                windowL++;
            }
            ans+=windowR-windowL;
        }
        long[] help=new long[R-L+1];
        int i=0;
        int p1=L;
        int p2=M+1;
        while(p1<=M && p2<=R){
            help[i++]=sum[p1]<=sum[p2]?sum[p1++]:sum[p2++];
        }
        while(p1<=M){
            help[i++]=sum[p1++];
        }
        while(p2<=R){
            help[i++]=sum[p2++];
        }
        for(i=0; i<help.length; i++){
            sum[L+i]=help[i];
        }
        return ans;
    }
}
相关文章
|
3天前
|
数据采集 人工智能 安全
|
13天前
|
云安全 监控 安全
|
4天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
1085 152
|
18天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1751 9
|
9天前
|
人工智能 自然语言处理 API
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸
一句话生成拓扑图!next-ai-draw-io 结合 AI 与 Draw.io,通过自然语言秒出架构图,支持私有部署、免费大模型接口,彻底解放生产力,绘图效率直接爆炸。
694 152
|
11天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
660 14
|
6天前
|
SQL 自然语言处理 调度
Agent Skills 的一次工程实践
**本文采用 Agent Skills 实现整体智能体**,开发框架采用 AgentScope,模型使用 **qwen3-max**。Agent Skills 是 Anthropic 新推出的一种有别于mcp server的一种开发方式,用于为 AI **引入可共享的专业技能**。经验封装到**可发现、可复用的能力单元**中,每个技能以文件夹形式存在,包含特定任务的指导性说明(SKILL.md 文件)、脚本代码和资源等 。大模型可以根据需要动态加载这些技能,从而扩展自身的功能。目前不少国内外的一些框架也开始支持此种的开发方式,详细介绍如下。
438 5