#209 Minimum Size Subarray Sum

COMSOL相场法模拟枝晶生长:溶质偏析模型搭建全流程 凝固过程中枝晶形貌与溶质偏析直接决定铸件微观组织与力学性能,是材料加工领域长期关注的核心问题。相场法凭借弥散界面描述天然处理拓扑演化,成为模拟枝晶生长的主流数值方法,而通用有限元平台COMSOL为自定义偏微分方程求解提供了高度灵活的实现路径。从热力学驱动、界面各向异性到溶质再分配,模型需准确耦合相场方程与浓度扩散方程,才能在二元合金凝固模拟中还原真实枝晶尖端速度、二次臂形貌及界面富集行为。围绕溶质偏析这一关键变量,建模过程涉及无量纲化参数标定、反溶质截留项配置、分离式求解器与网格各向异性控制等核心细节。该方 阅读详情

 

题目:

 

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.

题解:

 

 

public class Solution {
    public int minSubArrayLen(int s, int[] nums) {
        Arrays.sort(nums);
        int sum=0;
        for(int i=nums.length-1;i>=0;i--){
            sum+=nums[i];
            if(sum>=s)
                return(nums.length-i);
        }
        return 0;
    }
}


上面给出的是错误的解法,难怪会觉得这么简单,题目看错了。

 

要求的subarray是连续的,上面的代码只是求了一个sum>=s的一个组合,对连续性没有给出限制。

 

正解:这是一个典型的滑动窗口问题。两个指针, start end, end向后走,直到 sum 大于 s. 然后start向后, 直到sum 小于s. 同时更新 min值。这样其实是比较了每一个以start开始的最小长度。

 

public class Solution {
    public int minSubArrayLen(int s, int[] nums) {
        int start = 0;
        int end = 0;        
        int sum = 0;
        int min = Integer.MAX_VALUE;
        
        while(start<nums.length && end<nums.length) {
            while(sum<s && end<nums.length) {
                sum += nums[end++];
            }
            while(sum>=s && start<=end) {
                min = Math.min(min, end-start);
                sum -= nums[start++];
            }
        }
        return min==Integer.MAX_VALUE ? 0 : min;
    }
}


这题还有个O(nlgn)的解法,用的是二分查找法。因为nums数组都是正数,在不下降的序列中寻找恰好比s小的数出现的位置。思路如下:建立一个比原数组长一位的sums数组,其中sums[i]表示nums数组中[0, i - 1]的和,然后我们对于sums中每一个值sums[i],用二分查找法找到子数组的右边界位置,使该子数组之和大于sums[i] + s,然后我们更新最短长度的距离即可。

 

理解有点难度,我也没写出来,下面的代码是网上down的,有时间多看几遍熟悉熟悉。

 

// O(nlgn)
class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        int len = nums.size(), sums[len + 1] = {0}, res = len + 1;
        for (int i = 1; i < len + 1; ++i) sums[i] = sums[i - 1] + nums[i - 1];
        for (int i = 0; i < len + 1; ++i) {
            int right = searchRight(i + 1, len, sums[i] + s, sums);
            if (right == len + 1) break;
            if (res > right - i) res = right - i;
        }
        return res == len + 1 ? 0 : res;
    }
    int searchRight(int left, int right, int key, int sums[]) {
        while (left <= right) {
            int mid = (left + right) / 2;
            if (sums[mid] >= key) right = mid - 1;
            else left = mid + 1;
        }
        return left;
    }
}
【49】Camunda8-Self-Managed部署之Docker方式 围绕camunda8 self-managed,介绍了基于Docker部署相关服务的过程。 阅读详情

相关推荐

插件系统架构解析:VS Code与Obsidian设计对比

插件系统是现代软件开发中实现功能扩展的核心机制,其本质是通过标准化接口实现宿主程序与扩展模块的解耦。从技术原理看,典型的插件架构包含宿主环境、接口契约和功能实现三个关键组件,这种设计模式在VS Code、Obsidian等主流编辑器中得到广泛应用。在工程实践中,不同系统对接口标准化程度的要求差异显著:VS Code采用严格的package.json声明规范,而Obsidian则支持更灵活的manifest配置方式。良好的插件架构能显著提升软件的可维护性和生态活跃度,特别是在需要频繁功能迭代的开发工具领域。本

weixin_31822733的博客 269

Leetcode #209 Minimum Size Subarray Sum

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

王旻宇 406

Windows 高精度计时器:QueryPerformanceCounter 原理、陷阱与最佳实践

本文深入解析Windows高精度计时器QueryPerformanceCounter(QPC)的工作原理、常见陷阱及最佳实践。QPC通过读取硬件计时器(如TSC)实现微秒级精度,适用于游戏开发、音视频处理等高性能场景。文章详细探讨了多核同步、动态频率调整等挑战,并提供了优化代码示例和性能对比数据。

weixin_33391446的博客 273

LeetCode #209 - Minimum Size Subarray Sum

题目描述: 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. If there isn't one, return 0 instead. Example:  Input: ...

LawFile的博客 116

共聚焦显微镜的光源、工作原理与选型

共聚焦显微镜作为科研与工业领域中重要的微观成像工具,凭借光源的精准适配性、光学切片技术的三维成像能力,以及对不同工业场景的定制化适配潜力,为半导体检测、工业部件分析、智能制造升级提供了关键技术支撑。深入理解其光源特性、工作原理与选型逻辑,能帮助工程师与技术人员精准匹配设备与生产需求,既保障微观检测的精度与效率,又为产业优化、技术创新筑牢微观观测的基础。

PhotonixBay的博客 1540

Qt + windows + Linux+QtInstallerFramework打包教程

本文介绍了使用Qt Installer Framework在Windows和Linux系统下打包Qt应用程序的完整教程。Windows部分详细说明了从下载安装框架、配置环境变量、生成release版本,到创建安装包目录结构和修改配置文件的完整流程。Linux部分则重点讲解了环境变量设置、桌面应用配置(.desktop文件)以及两种打包方案:一种使用linuxdeployqt生成AppImage文件,另一种采用类似Windows的安装包方式。教程提供了关键配置文件(config.xml、package.xml

Qt历险记,专注Qt技术,三年医疗设备研发经验,1年机器视觉研发经验,半年柔性传感研发经验。 517

Leetcode日练笔记26 #561 #209 Array Partition I & Minimum Size Subarray Sum

#561 Array Partition I Given an integer arraynumsof2nintegers, group these integers intonpairs(a1, b1), (a2, b2), ..., (an, bn)such that the sum ofmin(ai, bi)for alliismaximized. Returnthe maximized sum. 解题思路: 能获得pair里min之和的最大值策略是从小到大排列,...

Bittersweet_t的博客 238

YOLOv11改进 - 卷积Conv | PATConv(Partial Attention Convolution)部分注意力卷积,在减少计算量的同时融合卷积与注意力的优势 | AAAI 2026

本文提出部分注意力卷积(PATConv)机制,并将其集成到YOLOv11中。传统神经网络中,卷积计算密集,注意力机制全局计算冗余,此前的“部分卷积”会丢失未计算通道的特征价值。PATConv通过“通道拆分 - 并行处理 - 结果拼接”的逻辑,给不同通道分配“擅长的任务”,兼顾局部与全局特征。基于此,它还衍生出PAT_ch、PAT_sp和PAT_sf三种细分模块。此外,还提出了动态部分卷积(DPConv),并构建了新的混合网络家族PartialNet。我们将PATConv代码集成到YOLOv11中,实验表明,

魔改工程师的博客 1085

[leetcode] 209. Minimum Size Subarray Sum @ python

原题 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. If there isn’t one, return 0 instead. Example: Input: s = 7, n...

闲庭信步 758

小孙学变频学习笔记(二)变频器的逆变电路

本文主要介绍了变频器中逆变电路的核心器件IGBT及其应用。IGBT是一种电压控制型三端半导体开关器件,具有开关频率高、功耗低的优点,在变频器中仅使用截止和饱和导通状态。文章详细分析了IGBT的结构特性、与晶闸管的区别,以及由四个IGBT组成的直流转交流逆变电路原理。重点阐述了IGBT并联反向二极管的作用:提供电流反向通路、避免反向电压损坏器件、提高开关效率等。最后提供了相关学习参考网址供读者进一步研究。

qq_43091070的博客 1199

关节电机技术全解析:原理、双编码器、控制方法与硬件选型

关于关机电机的组成,技术原理,特点以及选型等方面的详细介绍,可以帮助快速学习关节电机相关内容!

start_up_go的博客 698

Leetcode 209.长度最小的子数组(Minimum Size Subarray Sum)

Leetcode 209.长度最小的子数组 1 题目描述(Leetcode题目链接)   给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。 输入: s = 7, nums = [2,3,1,2,4,3] 输出: 2 解释: 子数组 [4,3] 是该条件下的长度最小的连续子数组。   进阶:如果你已...

qq_39378221的博客 339

【毕设救星】从0到1打造YOLOv8货架商品检测与可视化系统,让你的毕设脱颖而出

本文介绍了一个基于YOLOv8的货架商品检测系统毕设项目。系统采用YOLOv8模型进行商品检测,包含环境搭建、数据集准备、模型训练与验证等完整流程,并开发了可视化UI界面。项目聚焦智能零售应用场景,技术栈完整且易于实现,适合作为计算机视觉方向的毕业设计。文章详细指导了从数据标注、模型训练到界面开发的全过程,提供代码示例和实用建议,帮助学生快速完成毕设并提升答辩表现。

LXY_LOVE36的博客 104

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, retu...

weixin_41838371的博客 115

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

leetcode 209. Minimum Size Subarray Sum

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. If there isn’t one, return 0 instead. Example: Input: s = 7, nums...

zhpf225的博客 228

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

leetcode_209. Minimum Size Subarray Sum

https://leetcode.com/problems/minimum-size-subarray-sum/#/description Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the

郭大帅 360

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

[leetcode] 209. Minimum Size Subarray Sum

Description 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. If there isn’t one, return 0 instead. Example: Input:...

Learning from the mistakes 163

LeetCode 209:最小长度的子数组 Minimum Size Subarray Sum

公众号: 爱写bug(ID:icodebugs) 作者:爱写bug 给定一个含有 n 个正整数的数组和一个正整数 **s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。**如果不存在符合条件的连续子数组,返回 0。 Given an array of n positive integers and a positive integer s, find the minimal leng...

爱写Bug 223

[LeetCode]209. Minimum Size Subarray Sum

https://leetcode.com/problems/minimum-size-subarray-sum/#/description 找到子数组的和大于等于s的最短子数组 非常简单,找到满足条件的边界条件,不断update public class Solution { public int minSubArrayLen(int s, int[]

gqk289的专栏 526

[leetcode]209. Minimum Size Subarray Sum

题目链接:https://leetcode.com/problems/minimum-size-subarray-sum/#/description Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of whic

xiaocong1990的博客 277

【Leetcode】209. 长度最小的子数组(Minimum Size Subarray Sum)

一、题目 给定一个含有 n 个正整数的数组和一个正整数 target 。 找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, …, numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。 二、示例 示例 1: 输入:target = 7, nums = [2,3,1,2,4,3] 输出:2 解释:子数组 [4,3] 是该条件下的长度最小的子数组。 示例 2: 输入:target = 4, nums = [1,4,4] 输出:1

Xiao_Spring的博客 259

Leetcode-209 长度最小的子数组 ----巧用双指针和滑动窗口 [Minimum Size Subarray Sum]

Leetcode-209 [Minimum Size Subarray Sum] 长度最小的子数组 题目描述: 给定一个含有 n 个正整数的数组 nums 和一个正整数 target ,找出该数组中满足其和 ≥ target 的长度最小的 连续 子数组,并返回其长度。如果不存在符合条件的子数组,返回 0。 示例: 输入:target = 7, nums = [2,3,1,2,4,3] 输出:2 解释:子数组 [4,3] 是该条件下的长度最小的子数组。 题目链接: https://leetcode-cn.co

weixin_42252533的博客 199

java 数组截取_LeetCode 209:最小长度的子数组 Minimum Size Subarray Sum

算法是一个程序的灵魂公众号:爱写bug(ID:icodebugs)作者:爱写bug给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。Given an array of n positive integers and a positive integer s, find the minimal leng...

weixin_42515841的博客 148
上一篇: #217 Contains Duplicate
下一篇: #112 Path Sum
private_void
博客等级 码龄15年 3粉丝 33原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值