当前位置:网站首页>「面试高频题」难度大 1.5/5,经典「前缀和 + 二分」运用题
「面试高频题」难度大 1.5/5,经典「前缀和 + 二分」运用题
2022-07-02 06:33:00 【程序员柒柒】
题目描述
这是 LeetCode 上的 209. 长度最小的子数组 ,难度为 中等。
Tag : 「前缀和」、「二分」
给定一个含有 n 个正整数的数组和一个正整数 target。
找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl,numsl+1,...,numsr−1,numsr][nums_l, nums_{l+1}, ..., nums_{r-1}, nums_r][numsl,numsl+1,...,numsr−1,numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 000 。
示例 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
复制代码提示:
- 1<=target<=1091 <= target <= 10^91<=target<=109
- 1<=nums.length<=1051 <= nums.length <= 10^51<=nums.length<=105
- 1<=nums[i]<=1051 <= nums[i] <= 10^51<=nums[i]<=105
前缀和 + 二分
利用 nums[i]nums[i]nums[i] 的数据范围为 [1,105][1, 10^5][1,105],可知前缀和数组满足「单调递增」。
我们先预处理出前缀和数组 sum(前缀和数组下标默认从 111 开始),对于每个人 nums[i]nums[i]nums[i] 而言,假设其对应的前缀和值为 s=sum[i+1]s = sum[i + 1]s=sum[i+1],我们将 nums[i]nums[i]nums[i] 视为数组的右端点,问题转换为:在前缀和数组下标 [0,i][0, i][0,i] 范围内找到满足 值小于等于 s−ts - ts−t 的最大下标,充当数组左端点的前一个值。
利用前缀和数组的「单调递增」(即具有二段性),该操作可使用「二分」来做。
代码:
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;
}
}
复制代码- 时间复杂度:预处理前缀和数组的复杂度为 O(n)O(n)O(n),遍历前缀和数组统计答案复杂度为 O(nlogn)O(n\log{n})O(nlogn)。整体复杂度为 O(nlogn)O(n\log{n})O(nlogn)
- 空间复杂度:O(n)O(n)O(n)
最后
这是我们「刷穿 LeetCode」系列文章的第 No.209 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板
总结
仓库:github.com/SharingSour… 。
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
边栏推荐
猜你喜欢

Qt——如何在QWidget中设置阴影效果

C4D quick start tutorial - Chamfer

远程连接IBM MQ报错AMQ4036解决方法

Solution and analysis of Hanoi Tower problem

Sqli labs level 8 (Boolean blind note)

Synchronize files using unison

OpenFeign 简单使用

C language implementation of mine sweeping game

OpenShift 容器平台社区版 OKD 4.10.0部署

What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
随机推荐
Pclpy projection filter -- projection of point cloud to cylinder
win10使用docker拉取redis镜像报错read-only file system: unknown
Openshift build image
gocv图片裁剪并展示
Dip1000 implicitly tagged with fields
2022/2/14 summary
Use of libusb
Linux二进制安装Oracle Database 19c
C# 高德地图 根据经纬度获取地址
Count the number of various characters in the string
ORA-12514问题解决方法
NPOI 导出Word 字号对应
Sqli labs (post type injection)
使用IBM MQ远程连接时报错AMQ 4043解决思路
Pyspark de duplication dropduplicates, distinct; withColumn、lit、col; unionByName、groupBy
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
C4D quick start tutorial - C4d mapping
PCL calculates the intersection of three mutually nonparallel planes
Using recursive functions to solve the inverse problem of strings
C#钉钉开发:取得所有员工通讯录和发送工作通知