news 2026/4/23 13:03:56

LeetCode 3013.将数组分成最小总代价的子数组 II:两个堆维护k-1小 + 滑动窗口

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
LeetCode 3013.将数组分成最小总代价的子数组 II:两个堆维护k-1小 + 滑动窗口

【LetMeFly】3013.将数组分成最小总代价的子数组 II:两个堆维护k-1小 + 滑动窗口

力扣题目链接:https://leetcode.cn/problems/divide-an-array-into-subarrays-with-minimum-cost-ii/

给你一个下标从0开始长度为n的整数数组nums和两个整数kdist

一个数组的代价是数组中的第一个元素。比方说,[1,2,3]的代价为1[3,4,1]的代价为3

你需要将nums分割成k连续且互不相交的子数组,满足第二个子数组与第k个子数组中第一个元素的下标距离不超过dist。换句话说,如果你将nums分割成子数组nums[0..(i1- 1)], nums[i1..(i2- 1)], ..., nums[ik-1..(n - 1)],那么它需要满足ik-1- i1<= dist

请你返回这些子数组的最小总代价。

示例 1:

输入:nums = [1,3,2,6,4,2], k = 3, dist = 3输出:5解释:将数组分割成 3 个子数组的最优方案是:[1,3] ,[2,6,4] 和 [2] 。这是一个合法分割,因为 ik-1- i1等于 5 - 2 = 3 ,等于 dist 。总代价为 nums[0] + nums[2] + nums[5] ,也就是 1 + 2 + 2 = 5 。 5 是分割成 3 个子数组的最小总代价。

示例 2:

输入:nums = [10,1,2,2,2,1], k = 4, dist = 3输出:15解释:将数组分割成 4 个子数组的最优方案是:[10] ,[1] ,[2] 和 [2,2,1] 。这是一个合法分割,因为 ik-1- i1等于 3 - 1 = 2 ,小于 dist 。总代价为 nums[0] + nums[1] + nums[2] + nums[3] ,也就是 10 + 1 + 2 + 2 = 15 。 分割 [10] ,[1] ,[2,2,2] 和 [1] 不是一个合法分割,因为 ik-1和 i1的差为 5 - 1 = 4 ,大于 dist 。 15 是分割成 4 个子数组的最小总代价。

示例 3:

输入:nums = [10,8,18,9], k = 3, dist = 1输出:36解释:将数组分割成 4 个子数组的最优方案是:[10] ,[8] 和 [18,9] 。这是一个合法分割,因为 ik-1- i1等于 2 - 1 = 1 ,等于 dist 。总代价为 nums[0] + nums[1] + nums[2] ,也就是 10 + 8 + 18 = 36 。 分割 [10] ,[8,18] 和 [9] 不是一个合法分割,因为 ik-1和 i1的差为 3 - 1 = 2 ,大于 dist 。 36 是分割成 3 个子数组的最小总代价。

提示:

  • 3 <= n <= 105
  • 1 <= nums[i] <= 109
  • 3 <= k <= n
  • k - 2 <= dist <= n - 2

解题方法:有序集合 + 滑动窗口

n u m s numsnums第一个元素必选,剩下k − 1 k-1k1个元素的起始位置间隔必须≤ d i s t \leq distdist。使用一个大小为d i s t + 1 dist+1dist+1的滑动窗口,每次求这个窗口中k − 1 k-1k1小元素的和。

问题变成了滑动窗口向右滑动过程中,窗口中不断新增一个元素、移除一个元素的状态下如何保持计算k − 1 k-1k1小的元素有哪些:

我们可以使用两个有序集合,一个叫s t a g e stagestage代表(正在舞台上的)k − 1 k-1k1小元素,一个叫c a n d i d a t e candidatecandidate代表在窗口中但(暂)未被选中的元素。

窗口右移过程中,假设要新加入窗口的元素是i n inin,移除窗口的元素是o u t outout

对于o u t outout有:

  • 如果o u t outoutc a n d i d a t e candidatecandidate候选集合中,那么o u t outout永无上台之日,直接移出候选
  • 如果o u t outouts t a g e stagestage选中集合中,那么o u t outout是时候退役了,移出s t a g e stagestage集合,并更新集合中元素之和

对于i n inin有:

  • 如果i n inins t a g e stagestage中最大元素小,说明更优秀的人来了,移出s t a g e stagestage集合中最大的那个,将i n inin加入s t a g e stagestage集合,并更新s t a g e stagestage集合中元素之和
  • 否则,将i n inin加入候选

之后进行s t a g e stagestage集合大小的调整:

  • 如果s t a g e stagestage集合小于k − 1 k-1k1,说明有人退役暂无人顶上,从c a n d i d a t e candidatecandidate中选最小的那个顶上(移出c a n d i d a t e candidatecandidate并加入s t a g e stagestage),并更新s t a g e stagestage集合中元素之和
  • 如果s t a g e stagestage集合大于k + 1 k+1k+1,说明有更优秀的人来了,要把s t a g e stagestage中最大的那个移出并加入到c a n d i d a t e candidatecandidate,并更新s t a g e stagestage集合中元素之和

整个滑动窗口移动的过程中,所有s t a g e stagestage元素和中最小的那个即为答案。

为了方便计算,我们开局直接把k kk减一。

  • 时间复杂度O ( n log ⁡ d i s t ) O(n\log dist)O(nlogdist)
  • 空间复杂度O ( d i s t ) O(dist)O(dist)

AC代码

C++
/* * @LastEditTime: 2026-02-03 22:11:11 */typedeflonglongll;classSolution{public:llminimumCost(vector<int>&nums,intk,intdist){k--;multiset<ll>stage(nums.begin()+1,nums.begin()+dist+2),candidate;ll ans=accumulate(nums.begin(),nums.begin()+dist+2,0ll);while(stage.size()>k){intretiree=*stage.rbegin();stage.erase(prev(stage.end()));ans-=retiree;candidate.insert(retiree);}ll nowAns=ans;for(intend=dist+2;end<nums.size();end++){intin=nums[end],out=nums[end-dist-1];// outmultiset<ll>::iterator it=candidate.find(out);if(it!=candidate.end()){candidate.erase(it);}else{stage.erase(stage.find(out));nowAns-=out;}// inif(in<*stage.rbegin()){stage.insert(in);nowAns+=in;}else{candidate.insert(in);}// resizeif(stage.size()==k-1){intnewActor=*candidate.begin();candidate.erase(candidate.begin());stage.insert(newActor);nowAns+=newActor;}elseif(stage.size()==k+1){intretiree=*stage.rbegin();stage.erase(prev(stage.end()));nowAns-=retiree;candidate.insert(retiree);}ans=min(ans,nowAns);}returnans;}};#ifdefined(_WIN32)||defined(__APPLE__)/* [1,3,2,6,4,2] 3 3 5 */intmain(){string s;inta,b;while(cin>>s>>a>>b){Solution sol;vector<int>v=stringToVector(s);cout<<sol.minimumCost(v,a,b)<<endl;}return0;}#endif

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/22 21:12:18

当AI奖励模型开始“偷懒“:字节跳动如何让它们跟上AI助手的步伐

这是一项由字节跳动、北京航空航天大学、清华大学、人民大学、香港中文大学等多家机构联合完成的研究&#xff0c;发表于2026年2月。论文提出了R2M&#xff08;实时对齐奖励模型&#xff09;框架&#xff0c;论文编号为arXiv:2601.22664v1。有兴趣深入了解的读者可以通过这个编…

作者头像 李华
网站建设 2026/4/23 9:41:12

Windows休眠故障再次复发:微软补丁周二遭遇“土拨鼠日“

微软在一月底宣布&#xff0c;此前声称已通过带外更新修复的休眠问题再次影响更多设备。 一月三十日&#xff0c;微软通过发布健康仪表板承认问题仍然存在。该公司指出&#xff0c;启用了虚拟安全模式&#xff08;VSM&#xff09;且支持安全启动功能的电脑"同样受到此问题…

作者头像 李华
网站建设 2026/4/23 9:43:01

【苹果手机游戏推荐】数独【79.1 MB】

链接&#xff1a;https://pan.quark.cn/s/83aec32583c6 游戏简介 趣味数独大战&#xff0c;创意逻辑推理游戏&#xff0c;考验你大脑的极限&#xff0c;挑战你智商的上限。 数独经典玩法&#xff1a; 四宫/六宫&#xff1a;初级简单数独&#xff0c;只需要在1-4的随机数字中…

作者头像 李华
网站建设 2026/4/23 9:41:07

从入门到大神:提示工程架构师应对Agentic AI技术挑战

从入门到大神&#xff1a;提示工程架构师的Agentic AI挑战应对指南 备选标题 《Agentic AI时代&#xff1a;提示工程架构师的进阶之路与挑战破解》《提示工程Agentic AI&#xff1a;从新手到专家的全链路能力升级手册》《应对Agentic AI挑战&#xff1a;提示工程架构师的入门…

作者头像 李华
网站建设 2026/4/23 9:37:33

mellanox onyx 交换机配置 snmp 监控 snmp_exporter 采集到 prometheus

开启 web 服务用来给 onyx 下载镜像用 #主机 192.168.0.100 操作 chmod 777 /tmp/snmp-exporter:latest.tar.gz python3 -m http.server 8080启动 snmp-exporter docker 容器 #交换机操作 onyx 系统 image fetch http://192.168.0.100:8080/snmp-exporter:latest.tar.gz do…

作者头像 李华