Leetcode209-Minimum Size Subarray Sum

《华为项目管理之道》第1章笔记 performance indicator,KPI)纳入个人绩效承诺书,贯彻结果导向的理念,以向员工。客户需求是华为发展的原动力,为客户服务是华为存在的唯一理由。目经理‘用兵’,依靠市场机制‘调兵’”,打破了人员在功能部门的板结,实现了听得。为管理者选拔的必要条件,项目经理成为人人向往的岗位。《华为项目管理之道》,是新出的华为官方的项目管理书,整个书不错。伴随着项目型组织的建立,华为逐步形成了完备的项目管理流程和制度,从而将业务运。为此,通过改革,重新明确了项目经理承担的经营责任,并开始以项。 阅读详情

新的个人博客:Hu Haoyu’s Blog,欢迎参观!

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn’t one, return 0 instead.

For example, given the array [2,3,1,2,4,3] and s = 7,
the subarray [4,3] has the minimal length under the problem constraint.

题意

给出一个包含n个正整数的数组和一个正整数s,找到长度最小的(连续)子数组使其和大于等于s。如果不存在这样的子数组,返回0。

比如数组为[2, 3, 1, 2, 4, 3],s = 7。子数组[4, 3]是长度最小的子数组,其和4+3≥7。

分析

使用一种在线处理的方法,类似“数组的最大子数组和”的O(n)解法。

步骤

  • 我们设置bottom和top控制子数组的头部和尾部。
  • 初始化bottom=0,top为-1,表示一个空的子数组。
  • 子数组和sum=0,最小长度len=0。
  • 当sum < s时,在当前子数组的尾部增加一个元素bottom[++top]。
  • 当sum ≥ s时,去掉当前子数组的头部元素,并++bottom。
  • 退出循环的条件:top == nums.size() 或 bottom>top(此时已经存在最小len为1,不可能更小,可以退出)。

算法复杂度

由于top和bottom至多遍历一次数组nums,因此算法复杂度为O(n)。

更多练习

题目要求再给出一种O(nlogn)的解法。

简略分析

采用分治法的思想。每次将区间A一分为二,成为A1和A2。子问题是求两个子区间A1和A2中的各自的最小子数组长度len1和len2,以及区间A的最小子数组长度len中的最小值,即min{len1, len2, len}。

算法复杂度

由主定理(master定理)可知:T(n) = 2*T(n/2) + n,故算法复杂度为O(nlogn)。

AC代码

O(n)及O(nlogn)算法

//O(n)
class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        if (!nums.size()) return 0;
        int bottom = 0, top = -1;
        int sum = 0, len = 0;
        while (true) {
            if (sum < s) {
                ++top;
                if (top != nums.size())
                    sum += nums[top];
                else
                    break;
            } else {
                sum -= nums[bottom]; ++bottom;
                if (bottom > top)
                    break;
            }
            if (sum >= s) {
                int new_len = top - bottom + 1;
                if (!len || len && new_len < len)
                    len = new_len;
            }
        }
        return len;
    }
};

//O(nlogn)
class Solution {
public:
    int MAX_INT = 999999999;
    int minSubArrayLen(int s, vector<int>& nums) {
        if (!nums.size()) return 0;
        return findMinLen(s, nums, 0, nums.size() - 1);
    }
    int findMinLen(int s, vector<int>& nums, int bottom, int top) {
        if (top == bottom) return nums[top] >= s ? 1 : 0;
        int mid = (bottom + top) / 2;
        int left = mid, right = mid + 1, sum = 0, len;
        while (sum < s && (right <= top || left >= bottom)) {
            if  (right > top) {
                sum += nums[left]; --left;
            }
            else if (left < bottom) {
                sum += nums[right]; ++right;
            }
            else if (nums[left] > nums[right]) {
                sum += nums[left]; --left;
            }
            else {
                sum += nums[right]; ++right;
            }
        }
        if (sum >= s) {
            len = right - left - 1;
            int leftLen = findMinLen(s, nums, bottom, mid);
            int rightLen = findMinLen(s, nums, mid + 1, top);
            return minValue(leftLen, rightLen, len);
        }
        else {
            return 0;
        }
    }
    int minValue(int x, int y, int z) {
        if (!x) x = MAX_INT;
        if (!y) y = MAX_INT;
        if (x <= y && x <= z) return x;
        if (y <= x && y <= z) return y;
        return z;
    }
};

如分析或代码有误,请批评指正,谢谢。

C语言实现惯性导航初始对准全部核心代码 惯性导航初始对准包括间接粗对准和卡尔曼滤波精对准,其中。为基于卡尔曼滤波的精对准,其中。 阅读详情

相关推荐

雷达信号处理DDC数字下变频

由于接收到的雷达信号频率较高,不能对其进行直接采样并数据处理,所以需要对雷达信号进行预处理,在降低其采样率的同时,将其由中频降低到低频,然后对输出的基带信号进行后续的解析,以便于获取雷达信号的特征参数。 本文采用数字正交下变频,将单通道信号转化为正交双通道信号,数字下变频(DDC)的原型为模拟下变频,通过对窄带雷达信号或单频信号进行数字混频,然后对信号进行正交采样,提取 I、Q 两路正交分量,最后通过滤波去除信号中的高频分量,将信号由中高频搬移至低频。 模拟下变频的系统框图如下: 其数学推导.

qq_44033461的博客 5228

LeetCode 209---Minimum Size Subarray Sum

题目链接:LeetCode 209—Minimum Size Subarray Sum题目大意:求出正整数无序序列中长度最小的子序列的元素和等于目标数,如果没有这样的数,则返回0。实现代码:public class Problem209 { public static void main(String[] args) { // TODO Auto-generated metho

调包侠 272

BRAM在图像处理缓存中的实现:完整示例解析

深入讲解如何利用BRAM高效实现图像处理中的数据缓存,结合实际案例解析BRAM的应用优势与设计细节,提升系统性能。

weixin_29443363的博客 333

Leetcode 209 Minimum Size Subarray Sum

给出一个正整数数组,以及一个正整数s,找到最短的子数组且该子数组的和不小于s。如果不存在这样的子数组,返回0.

(ง •̀_•́)ง 570

EDSR图像超分辨程序复现(踩坑及解决记录)

EDSR图像超分辨程序复现(踩坑及解决记录)

Sunny_Coder的博客 3642

部署资源管理系统chemx

资源管理系统的部署教程

weixin_40784215的博客 822

[leetcode] 209 Minimum Size Subarray Sum

(一)O(logN)的解法 首先将原数组处理成前项和的形式,这样就保证了数组的有序(注意第一个是0,自己push进去),然后遍历数组,寻找小于等于sum[i]+s的最小的下标,如果找不到,那么break结束。否则继续下去,最后取最小值。 class Solution { public: int minSubArrayLen(int s, vector& nums) { vec

NK_test的博客 3368

C++设计模式之观察者模式

观察者模式(Observer Pattern)是一种行为设计模式,它定义了对象之间的一对多依赖关系,当一个对象状态发生变化时,所有依赖于它的对象都会得到通知并自动更新。这种模式通常用于实现分布式事件处理系统,当一个对象(称为“主题”或“发布者”)改变状态时,所有注册的观察者都会收到通知。

每天进步一点点。 600

leetcode 209 : Minimum Size Subarray Sum

leetcode : Minimum Size Subarray Sum

西施豆腐渣 9644

delphi 调用deepseek r1 api 并流式返回结果

摘要:本文展示了一个使用Delphi调用DeepSeek API的代码示例,包含两种调用方式:一次性获取结果和流式返回结果。代码实现了完整的API请求处理,包括HTTP客户端设置、JSON数据处理和UI界面交互。其中流式调用通过TStreamReader处理服务器发送事件(SSE),实时更新到TRichEdit控件并保持滚动条位置。文章还提到界面美化优化,为Delphi开发者提供了调用AI服务的实用参考方案。(150字)

全粘工程师 492

Hadoop在大数据领域的电商数据分析案例

随着电商行业的迅猛发展,每天都会产生海量的数据,如用户浏览记录、购买行为、商品信息等。如何从这些数据中提取有价值的信息,为电商企业的决策提供支持,成为了电商企业面临的重要问题。Hadoop作为一个开源的分布式计算平台,具有高可靠性、高扩展性和高效性等特点,能够很好地处理大数据。本文的目的就是通过实际案例,详细介绍Hadoop在电商数据分析中的应用,范围涵盖了从数据采集、存储到分析的整个流程。本文首先介绍背景信息,让读者了解Hadoop在电商数据分析中的重要性和应用场景。

Java大师兄的博客 1178

LeetCode209—Minimum Size Subarray Sum

终于不用找实习了,恢复一下学习的节奏。原题LeetCode209Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 inste

上善若水 638

计算机视觉卷积神经网络(CNN)基础:从LeNet到ResNet

计算机视觉卷积神经网络(CNN)基础:从LeNet到ResNet,在计算机视觉领域,卷积神经网络(Convolutional Neural Network,CNN)已然成为推动众多任务发展的核心力量。从早期简单的图像识别,到如今复杂的目标检测、语义分割、图像生成等任务,CNN 都展现出了卓越的性能。CNN 的发展历程见证了一系列经典模型的诞生,其中 LeNet 作为开山之作,为 CNN 的发展奠定了基础,而 ResNet 则通过创新性的设计,突破了传统神经网络的限制,使得网络能够构建得更深、性能更优。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 14万+

【Leetcode 209】Minimum Size Subarray Sum - MEDIUM

【Leetcode 209】Minimum Size Subarray Sum - MEDIUM题目思路题解反思复杂度分析 题目 Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. ...

weixin_43621681的博客 327

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

【leetcode】209. Minimum Size Subarray Sum

209.Minimum Size Subarray Sum Given an array ofnpositive integers and a positive integers, find the minimal length of acontiguoussubarray of which the sum ≥s. If there isn't one, return 0 inst...

lemonade13的博客 132

计算机控制技术及应用

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

力扣(LeetCode)209 长度最小的子数组 Minimum Size Subarray Sum

209 长度最小的子数组 Minimum Size Subarray Sum 看完这个题直接想到的是三层循环,先固定长度,再遍历,原谅我的愚蠢,我内心也觉得这个很可怕,然后写出来超时了。 然后看了有n的平方,然后想到可以先固定起始位置,再遍历,边遍历边检验: int minSubArrayLen(int target, int* nums, int numsSize){ int i, sum, j, minSublen = numsSize+1; for(i = 0; i < nu.

s_w_s_的博客 340

LeetCode - 209. Minimum Size Subarray Sum

题目 LeetCode - 209. Minimum Size Subarray Sum 题目链接 https://leetcode.com/problems/minimum-size-subarray-sum/ 参考博客 https://www.cnblogs.com/zjutlitao/p/3485545.html 解题思路 采用暴力解决,时间复杂度是O(n3)O(n^3)O(n3),这个基...

virgilshi的博客 177

LeetCode 209. Minimum Size Subarray Sum

欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-209-minimum-size-subarray-sum/ 解答1[Java]: class Solution { public int minSubArrayLen(int s, int[] nums) { int left = 0; int right ...

user2639的博客 146

NYUer | LeetCode209 Minimum Size Subarray Sum

NYUer | LeetCode209 Minimum Size Subarray Sum

Moses_SU的博客 272

leetcode: 209. Minimum Size Subarray Sum

leetcode: 209. Minimum Size Subarray Sum 原题链接 解法一:暴力,时间复杂度O(n^2) class Solution { public: int minSubArrayLen(int target, vector<int>& nums) { int n = INT32_MAX; // 不可能达到的数 for (int i = 0; i < nums.size(); i++) {

LSK的博客 158

解题思路:LeetCode 第 209 题 “Minimum Size Subarray Sum“

给定一个包含正整数的数组和一个正整数 `target`,找到该数组中满足其和大于等于 `target` 的最小长度子数组,并返回其长度。如果不存在这样的子数组,则返回 0。

侯仕奇的博客 608

LeetCode209. Minimum Size Subarray Sum

一 题目 Given an array ofnpositive integers and a positive integers, find the minimal length of acontiguoussubarray of which the sum ≥s. If there isn't one, return 0 instead. Example: Input: s...

bohu83的博客 302

leetcode209. Minimum Size Subarray Sum

建议再想一个时间复杂度是nlogn的方法。但是这题还提供了一个深入的跟进思考。暴力解法在leetcode中会超时。也可以用双指针滑动窗口来进行。可以用暴力解法来进行。以及双循环变量使用时。

ahahayaa的博客 537

LeetCode209 Minimum Size Subarray Sum

Given an array ofnpositive integers and a positive integers, find the minimal length of acontiguoussubarray of which the sum ≥s. If there isn't one, return 0 instead. Example: Input: s = 7, ...

李歇特冯-兹拜因巴哈的博客 104

Leetcode 209. Minimum Size Subarray Sum

Leetcode 209. Minimum Size Subarray Sum @(Algo Probs)[LeetCode, Algorithm, Two Pointers] Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous su...

TooSchoolForCool 403

[Leetcode 209, Medium] Minimum Size Subarray Sum

[Leetcode 209, Medium] Minimum Size Subarray Sum

算法的实践 1062
上一篇: Using Jekyll with Pages——使用Jekyll框架
下一篇: Leetcode210-Course Schedule II
coderhuhy
博客等级 码龄12年 24粉丝 40原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值