当前位置:网站首页>「面试高频题」难度大 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 原题链接和其他优选题解。
边栏推荐
- Solution of Xiaomi TV's inability to access computer shared files
- Googlenet network explanation and model building
- commands out of sync. did you run multiple statements at once
- Sqli labs level 1
- 一、Qt的核心类QObject
- gocv opencv exit status 3221225785
- QT drag event
- C language implementation of mine sweeping game
- Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
- Tensorflow2 keras classification model
猜你喜欢
Openshift build image
OpenFeign 簡單使用
Kubernetes deploys Loki logging system
队列的基本概念介绍以及典型应用示例
Redis安装部署(Windows/Linux)
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
Sentinel reports failed to fetch metric connection timeout and connection rejection
Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`
Linux安装Oracle Database 19c RAC
Linux二进制安装Oracle Database 19c
随机推荐
Qt——如何在QWidget中设置阴影效果
Installing Oracle database 19C for Linux
[blackmail virus data recovery] suffix Crylock blackmail virus
win10使用docker拉取redis镜像报错read-only file system: unknown
Image transformation, transpose
commands out of sync. did you run multiple statements at once
Openfeign is easy to use
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
gocv图片读取并展示
Leetcode sword finger offer brush questions - day 22
整理秒杀系统的面试必备!!!
gocv边界填充
History of Web Technology
Leetcode sword finger offer brush questions - day 23
Loadbalancer dynamically refreshes Nacos server
寻找链表中值域最小的节点并移到链表的最前面
Sqli labs level 8 (Boolean blind note)
Openshift deployment application
Linux安装Oracle Database 19c RAC
Gocv boundary fill