当前位置:网站首页>「面试高频题」难度大 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 原题链接和其他优选题解。
边栏推荐
- cmd窗口中中文呈现乱码解决方法
- C4D quick start tutorial - C4d mapping
- 【Go实战基础】gin 如何自定义和使用一个中间件
- C#钉钉开发:取得所有员工通讯录和发送工作通知
- Linux安装Oracle Database 19c RAC
- Don't spend money, spend an hour to build your own blog website
- Pclpy projection filter -- projection of point cloud to cylinder
- kubernetes部署loki日志系统
- gocv opencv exit status 3221225785
- win10使用docker拉取redis镜像报错read-only file system: unknown
猜你喜欢

Gateway is easy to use

Installing Oracle database 19C for Linux

What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?

Minecraft安装资源包

Service de groupe minecraft

Kubernetes deploys Loki logging system

【Go实战基础】gin 如何验证请求参数

Sqli labs level 12

Minecraft模组服开服

Detailed explanation of NIN network
随机推荐
QT -- how to set shadow effect in QWidget
Pyspark de duplication dropduplicates, distinct; withColumn、lit、col; unionByName、groupBy
Count the number of various characters in the string
Pclpy projection filter -- projection of point cloud to cylinder
gocv拆分颜色通道
寻找链表中值域最小的节点并移到链表的最前面
QT drag event
Avoid breaking changes caused by modifying constructor input parameters
Kubedm deploys kubernetes v1.23.5 cluster
Sentinel reports failed to fetch metric connection timeout and connection rejection
Gocv image cutting and display
Gocv split color channel
OpenFeign 简单使用
win10使用docker拉取redis镜像报错read-only file system: unknown
Judge whether it is Sudoku
ORA-12514问题解决方法
Kubesphere virtualization KSV installation experience
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
Multi version concurrency control mvcc of MySQL
队列的基本概念介绍以及典型应用示例