LintCode 404. Subarray Sum II (DP和同向双指针经典题)

企业数字化转型AI大模型赋能智能体平台及AI场景应用规划方案(生态合作版).pptx 立即下载
  1. Subarray Sum II

Given an integer array, find a subarray where the sum of numbers is in a given interval. Your code should return the number of possible answers. (The element in the array should be positive)

Example
Given [1,2,3,4] and interval = [1,3], return 4. The possible answers are:

[0, 0]
[0, 1]
[1, 1]
[2, 2]

解法1:
DP, 2重数组, 空间太大通不过。
代码如下:

class Solution {
public:
    /**
     * @param A: An integer array
     * @param start: An integer
     * @param end: An integer
     * @return: the number of possible answer
     */
    int subarraySumII(vector<int> &A, int start, int end) {
        int n = A.size();
        if (n == 0) return 0;
        
        int begin = 0, last = 0;
        int count = 0;
        vector<vector<int>> sums(n + 1, vector<int>(n + 1, 0));

        for (int i = 1; i <= n; ++i) {
            sums[1][i] = sums[1][i - 1] + A[i - 1];
            if (sums[1][i] >= start && sums[1][i] <= end) count++;
        }

        for (int i = 2; i <= n; ++i) {        
            sums[i][i] = A[i - 1];
            if (sums[i][i] >= start && sums[i][i] <= end) count++;
        }

        for (int i = 2; i <= n; ++i) {
            for (int j = i + 1; j <= n; ++j) {
               if (sums[i][j] == 0) {
                    sums[i][j] = sums[i][j - 1] + A[j - 1];
                    if (sums[i][j] >= start && sums[i][j] <= end) {
                        count++;
                    }
                }
            }    
        }
        
        return count;
    }
};

解法2:DP, 1维数组。
代码如下:

class Solution {
public:
    /**
     * @param A: An integer array
     * @param start: An integer
     * @param end: An integer
     * @return: the number of possible answer
     */
    int subarraySumII(vector<int> &A, int start, int end) {
        int n = A.size();
        if (n == 0) return 0;
        
        int begin = 0, last = 0;
        int count = 0;
        vector<int> sums(n + 1, 0);

        for (int i = 1; i <= n; ++i) {
            sums[1] = sums[1] + A[i - 1];
            if (sums[1] >= start && sums[1] <= end) count++;
        }

        for (int i = 2; i <= n; ++i) {        
            sums[i] = A[i - 1];
            if (sums[i] >= start && sums[i] <= end) count++;
        }

        for (int i = 2; i <= n; ++i) {
            for (int j = i + 1; j <= n; ++j) {
                sums[i] += A[j - 1];
                if (sums[i] >= start && sums[i] <= end) {
                    count++;
                }
            }    
        }
        
        return count;
    }
};

解法3:
presums[] + 同向双指针,分别求大于start-1和end的subarray个数,然后相减。时间复杂度O(n)。

代码如下:


class Solution {
public:
    /**
     * @param A: An integer array
     * @param start: An integer
     * @param end: An integer
     * @return: the number of possible answer
     */
    int subarraySumII(vector<int> &A, int start, int end) {
        int n = A.size();
        if (n == 0) return 0;
        if (end == 0) return 0;
        
        int p1 = 0, p2 = 1;
        int count = 0;
        vector<int> sums(n + 1, 0);
        for (int i = 1; i <= n; ++i) {
            sums[i] += sums[i - 1] + A[i - 1];
        }
        return largerSubarraySum(sums, start - 1) - largerSubarraySum(sums, end);

        return count;
    }

private:
    int largerSubarraySum(vector<int> &A, int target) {
        int n = A.size() - 1;  //注意这里要-1
        int p1 = 0, p2 = 1;
        int count = 0;
        while (p2 <= n && p1 <= p2) {
            if (A[p2] - A[p1] <= target) {
                p2++;
            } else {
                count += n - p2 + 1;   //重要!
                p1++;
            }
        }
        
        return count;
    }
};

解法4:二分查找。复杂度O(nlogn)。下次写。

MCGS的Http协议驱动,支持http协议 硬件配置 参数设置 设备属性 设备命令 通讯状态 附录 #HTTP 1、 设备简介 驱动类型 该驱动为独立设备,直接添加构件即可,不需要挂载到父设备下 通讯协议 采用标准HTTP1.1协议 通讯方式 驱动构件为客户端。 2、 硬件连接 本软件在通讯之前,请保证通讯连接正确。通讯连接方式:采用标准的RJ45接口的以太网接线(对调网线或使用HUB交换)。 3、 设备通讯参数 本设备为独立设备,无父设备参数 4、 设备构件参数设置 “HTTP设备”参数设置如下: <1> 内部属性: 构建url信息。 <2> 最小采集周期:本软件对设备进行操作的时间周期,单位为ms,默认为100ms。 <3> 通讯等待时间:通讯数据接收等待时间,默认设置为200ms,当采集速度要求较高或数据量较大时,设置值可适当减小或增大。 <4> HttpHead:http head添加需要的字段,不需要添加http head时值为空,默认不添加。 5、 设备命令 本设备构件提供设备命令,用于进行相应的操作,设备命令的格式如下: 设备命令 命令介绍 GetHttpHead 格式1 GetHttpHead(headStr) 用途 获取http head参数 参数 headStr 参数可以为字符型变量、字符串常量 例1 !SetDevice(设备0,6,"GetHttpHead(headStr)") 获取http head数据保存到headStr中 SetHttpHead 格式1 SetHttpHead(headStr) 用途 设置http head参数 参数 headStr 参数可以为字符型变量、字符串常量 立即下载

相关推荐

Computers & Security 投稿实战:从零到一详解避坑指南

本文详细解析了向《Computers & Security》投稿的全流程避坑指南,包括ORCID账号注册、文件格式统一、Editorial Manager®系统操作技巧等关键步骤。特别强调了cover letter撰写、Declaration of Interest Statement准备以及PDF生成后的检查要点,帮助研究者高效完成投稿流程。

weixin_29228203的博客 677

晨哥Leetcode 974. Subarray Sums Divisible by K

Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K. Example 1: Input: A = [4,5,0,-2,-3,1], K = 5 Output: 7 Explanation: There are 7 s...

weixin_45588823的博客 200

告别简陋行情!在Ubuntu24.04系统用Wine9.0跑同花顺远航版的全流程记录

本文详细记录了在Ubuntu 24.04系统上,通过Wine 9.0兼容层成功安装并流畅运行同花顺远航版的全过程。文章对比了该方案与官方简陋Linux原生版本的功能差异,重点解决了高分辨率屏幕下的字体与界面显示问题,并提供了关键配置调优与长期维护建议,为Linux用户提供了功能完备的专业金融行情解决方案。

weixin_29207533的博客 1078

138. Subarray Sum

138. Subarray Sum Description: Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number. Example...

Zahb44856的博客 272

Nordic nRF52832实战:手把手教你搭建智能手环BLE通信框架(附完整代码)

本文详细介绍了如何使用Nordic nRF52832芯片搭建智能手环的BLE通信框架,涵盖芯片优势、协议栈初始化、低功耗广播策略及健康数据服务实现。通过完整代码示例和架构设计,帮助开发者快速掌握蓝牙低功耗通信技术,适用于可穿戴设备开发。

weixin_29192141的博客 250

人形机器人硬件开发:骨架系统设计全攻略

本文以人形机器人骨架系统为核心研究对象,系统解析其从理论建模到工程实现的全流程设计方法。通过融合刚体力学、材料科学与机器人控制理论,构建"概念基础-理论框架-架构设计-实现机制-实际应用-高级考量-综合拓展"的七层分析体系。重点覆盖运动学/动力学建模、轻量化结构设计、关节模块集成、多物理场耦合优化等关键技术,结合波士顿动力Atlas、本田ASIMO等经典案例,提供从原型设计到量产验证的工程实践指南。

AI天才研究院 1881

经典算法题(二)——解Integer Array - 整型数组

本章主要总结与整型数组相关的题。 1.Remove Element Question leetcode: Remove Element | LeetCode OJ lintcode: (172) Remove Element Given an array and a value, remove all occurrences of that value in place and ...

m0_37442062的博客 2480

S7-1200与KINCO伺服通信实战:CM CANopen模块配置避坑指南(附完整PDO映射表)

本文详细解析了西门子S7-1200 PLC通过CM CANopen模块与KINCO伺服驱动器的通信配置全流程,重点解决PDO映射混乱、心跳报文配置错误等常见问题。提供硬件组态、参数设置、PDO映射模板及故障排查方案,助力工业自动化系统实现稳定高效的多轴运动控制。

2y3u4i5o6p的博客 1106

数据结构与算法--子数组和为k

问题:子数组和为kGiven an nonnegative integer array, find a subarray where the sum of numbers is k. Your code should return the index of the first number and the index of the last number.Example Given [1, 4, 2

往者不可谏,来者犹可追 603

【STK】手把手教你利用STK进行导弹和反导仿真04 - STK/MMT模块04 导弹飞行工具应用案例

(6)打开STK,导出的范围轮廓以我们定义的Iran区域目标不同的颜色显示,并将以对象浏览器中定义为区域目标。最后将生成的轨迹导出到STK进行显示和分析。(12)在场景中新创建两个Target,将它们命名为Target1和Target2,并将他们放置在发射的Launch的射程范围之内。(2)在STK中插入AreaTarget对象,在“插入STK对象”工具中,选择“区域目标”对象类型和“选择国家/地区”当前在“飞行列表”窗口中选择的飞行路径将显示较大的启动和目标图图标,并带有较粗的带注释的连接线。

weixin_44184852的博客 1704

PCL Super4PCS算法实现点云粗配准(版本一)【2025最新版】

Win11系统下实现[Super4PCS: Fast Global Pointcloud Registration via Smart Indexing一文中的配准算法。

点云侠的博客 3379

Subarray Sum II

Given an positive integer arrayAand an interval. Return the number of subarrays whose sum is in the range of given interval. Example Example 1: Input: A = [1, 2, 3, 4], start = 1, end = 3 Output...

leetcode 解题思路 463

【深度学习新浪潮】Qwen3.5-397B-A17B 深度解析与工程部署实战:超大MoE模型的推理加速与落地

Qwen3.5-397B-A17B是当前开源社区综合能力最强、部署效率最高的超大模型,其MoE架构彻底解决了“大模型难部署、慢、贵”的痛点。

专注:太空算力、AI infra、异构调度、大模型成本工程 1713

[LintCode] Subarray Sum II

Given an integer array, find a subarray where the sum of numbers is in a given interval. Your code should return the number of possible answers. (The element in the array should be positive) ...

164

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Codeforces 1644 C. Increase Subarray Sums —— 二维DP

This way 题意: 给你一个长度为n的整型数组a,和一个整型x。你可以在数组a中的任意多个位置加x,然后取出a中的连续一段使得数值总和最大,问你位置数从0~n答案一次是多少? 题解: 又咕了一段时间…真舒服啊(不是) 首先看到5e3当然想的就是暴力,但是发现暴力的话,要找左右端点,然后又要考虑数量,很容易就上N3N^3N3,然后不知道为什么就想到了DP。那么 dp[i][j]表示位置i作为取的末尾,j个位置加x的时候的答案是多少。那么状态转移方程就是: dp[i][j]=max(dp[i-1][j]

天翼之城的博客 451

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

LINTCODE:404 左叶子之和(Python语言实现)

题目描述 计算给定二叉树的所有左叶子之和。 示例 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是9和15,所以返回24。 递归 利用先序遍历的递归版筛选出符合要求的左叶子数。 class TreeNode: def __init__(self, x): self.val = x ...

王山山 203

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

LintCode 404.子数组求和Ⅱ

给定一个正整数数组 A 和一个区间. 返回和在给定区间范围内的子数组数量. **区间i到j的和等于 sum[j+1] - sum[i] sum[i]表示数组A中前i个元素的和 现在求start<=sum[j+1]-sum[i]<=end 可以通过遍历j 确定sum[i]的区间范围 去找j前面有多少个区间满足条件 找到大于左边界的最小元素下标和小于等于右边界的最大元素下标 为了用一个函数同时求两个下标(二分法) 可以直接求大于等于右边界+1的最小下标 即为j+1 直接与i作差就得到了区间长度 即满

weixin_50899769的博客 237

【Lintcode】404. Subarray Sum II

题目地址: https://www.lintcode.com/problem/subarray-sum-ii/description 给定一个正整数数组AAA,再给定一个区间[s,e][s,e][s,e],问AAA有多少个子数组之和位于那个区间内。 思路是前缀和。先把AAA的前缀和ppp算出来,其中p[i]=∑k=0i−1A[k]p[i]=\sum_{k=0}^{i-1}A[k]p[i]=∑k=0i−1​A[k],规定p[0]=0p[0]=0p[0]=0。对于i<ji<ji<j,就对应了一

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

LintCode 396: Coins in a Line III (博弈类DP经典题)

Coins in a Line III There are n coins in a line. Two players take turns to take a coin from one of the ends of the line until there are no more coins left. The player with the larger amount of money...

roufoo的博客 878

402.Continuous Subarray Sum-连续子数组求和(中等题)

连续子数组求和 题目给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大。输出答案时,请分别返回第一个数字和最后一个数字的下标。(如果两个相同的答案,请返回其中任意一个) 样例给定 [-3, 1, 3, -3, 4], 返回[1,4]. 题解当前子数组和sum如果小于0丢弃并以当前元素A[i]为sum,大于0则继续累加A[i],每次与A[i]运算完后与之前的sum进行比较,当前的sum大则

三色旗飞扬 921

[LintCode] Subarray Sum & Subarray Sum II

Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number. Example Given ...

weixin_34267123的博客 174

LintCode 1565: Modern Ludo I (DP典型题)

DP典型题。 需要注意: 起始点从1算起。 点2-7的DP值为1. 从点8到最后,所有点的DP值需要考虑两个因素:shortCut和骰子。 class Solution { public: /** * @param length: the length of board * @param connections: the connections of the pos...

roufoo的博客 771

Continuous Subarray Sum II

Description: Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Your code should return the index of the first number and the index of the last nu...

banhua3000的博客 144

Subarray Sum I/II/III

138. Subarray Sum Tag: Hash Table, Subarray. Main Idea: Problem Type. State: Update Function: Initialization: Answer: Tips/Notes: Time/Space Cost: Time Cost:  O(n)\ O(n) O(n) Code:...

Zahb44856的博客 343

TM4C129X LCD控制器核心配置:时序、DMA与中断详解

在嵌入式系统开发中,LCD驱动是连接微控制器与显示面板的关键环节,其核心在于建立一套精确的通信协议。理解其工作原理,需要从时序控制、DMA(直接内存访问)和中断管理这三个基础概念入手。时序控制定义了像素数据与同步信号(如行同步、场同步)的严格时间关系,是屏幕正确显示图像的物理基础。DMA技术则负责高效、自动地将帧缓冲区中的数据搬运至显示控制器,从而解放CPU,是处理高分辨率、高刷新率显示数据流的核心机制。中断系统提供了精准的事件通知,例如在每帧开始或DMA传输完成时触发,是实现双缓冲、避免屏幕撕裂以及进行系

weixin_30535843的博客 416
上一篇: LintCode 558: Sliding Window Matrix Maximum
下一篇: LintCode 400: Maximum Gap (bucket sort经典题!)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值