当前位置:网站首页>【面试高频题】难度 1.5/5,经典「前缀和 + 二分」运用题
【面试高频题】难度 1.5/5,经典「前缀和 + 二分」运用题
2022-06-21 17:50:00 【宫水三叶的刷题日记】
题目描述
这是 LeetCode 上的「209. 长度最小的子数组」,难度为「中等」。
Tag : 「前缀和」、「二分」
给定一个含有 n 个正整数的数组和一个正整数 target。
找出该数组中满足其和 ≥ target 的长度最小的 连续子数组
,并返回其长度。如果不存在符合条件的子数组,返回
。
示例 1:
输入:target = 7, nums = [2,3,1,2,4,3]
输出:2
解释:子数组 [4,3] 是该条件下的长度最小的子数组。
示例 2:
输入:target = 4, nums = [1,4,4]
输出:1
示例 3:
输入:target = 11, nums = [1,1,1,1,1,1,1,1]
输出:0
提示:
前缀和 + 二分
利用
的数据范围为
,可知前缀和数组满足「单调递增」。
我们先预处理出前缀和数组 sum(前缀和数组下标默认从
开始),对于每个
而言,假设其对应的前缀和值为
,我们将
视为子数组的右端点,问题转换为:在前缀和数组下标
范围内找到满足 「值小于等于
」 的最大下标,充当子数组左端点的前一个值。
利用前缀和数组的「单调递增」(即具有二段性),该操作可使用「二分」来做。
代码:
class Solution {
public int minSubArrayLen(int t, int[] nums) {
int n = nums.length, ans = n + 10;
int[] sum = new int[n + 10];
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + nums[i - 1];
for (int i = 1; i <= n; i++) {
int s = sum[i], d = s - t;
int l = 0, r = i;
while (l < r) {
int mid = l + r + 1 >> 1;
if (sum[mid] <= d) l = mid;
else r = mid - 1;
}
if (sum[r] <= d) ans = Math.min(ans, i - r);
}
return ans == n + 10 ? 0 : ans;
}
}
- 时间复杂度:预处理前缀和数组的复杂度为
,遍历前缀和数组统计答案复杂度为
。整体复杂度为
- 空间复杂度:
最后
这是我们「刷穿 LeetCode」系列文章的第 No.209 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
边栏推荐
- Deep Copy
- 2022中国眼博会,山东青少年眼睛健康展,视力矫正与康复展
- [AISI software] wechat applet development quotation scheme template
- MarkDown高级语法,兼容MarkText
- Three.js 3d粒子动画js特效代码
- Six steps of JDBC programming
- Canvas sphere particle changing color JS special effect
- Leetcode (210) - Schedule II
- 文件上传漏洞靶场分析 UPLOAD_LABS
- Delete the specified screen
猜你喜欢

一次 MySQL 误操作导致的事故,「高可用」都顶不住了!

Six steps of JDBC programming

Unity3D-后期处理 Post-process Volume Profile

图像分类、AI 与全自动性能测试

Must the database primary key be self incremented? What scenarios do not suggest self augmentation?

秒云云原生信创全兼容解决方案再升级,助力信创产业加速落地

canvas交互式颜色渐变js特效代码

Canvas dynamic background text luminous JS effect

From "village run enterprise" to "ten billion group", why did red star industry complete the "butterfly transformation"?

Full screen menu animation effect expansion in the upper left corner of SVG
随机推荐
蚂蚁集团自研TEE技术通过国家级金融科技产品认证 47项检测均达要求
C# Mapster 对象映射器学习
VsCode自定义模板,用模板记笔记?!
空中操作仅通过距离映射对遮挡目标进行鲁棒定位(RAL2022)
写着玩的处理框架
文件上传漏洞靶场分析 UPLOAD_LABS
老师们,oracle-cdc遇到不能解析的dml语句,因为这个语句里面有个字段是比较特殊的空间地理位
B-Tree
Niuke: merging two ordered arrays
How many correct answers can you get to Huawei Hongmeng certification test questions?
el-table分页全选功能讲解
# bash 的 try catch
挖财商学院属于证券公司吗?开户安全吗?
品牌、产品和服务齐发力,东风日产接下来有什么新动作?
6月22日直播 | 华南理工詹志辉: 面向昂贵优化的进化计算
Gartner 网络研讨会 “九问数字化转型” 会后感
Canvas dynamic mesh background JS effect
Cache design issues
From "village run enterprise" to "ten billion group", why did red star industry complete the "butterfly transformation"?
Ant group's self-developed tee technology has passed the national financial technology product certification, and 47 tests have met the requirements