当前位置:网站首页>「面试高频题」难度大 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 原题链接和其他优选题解。
边栏推荐
- 队列管理器running状态下无法查看通道
- Minecraft空岛服开服
- Don't spend money, spend an hour to build your own blog website
- Gateway is easy to use
- Using recursive functions to solve the inverse problem of strings
- gocv图片裁剪并展示
- Solution and analysis of Hanoi Tower problem
- Nacos download, start and configure MySQL database
- Minecraft plug-in service opening
- Npoi export word font size correspondence
猜你喜欢

Hengyuan cloud_ Can aiphacode replace programmers?

Tcp/ip - transport layer

Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel

【Go实战基础】gin 如何获取 GET 和 POST 的请求参数

Installing Oracle database 19C for Linux

小米电视不能访问电脑共享文件的解决方案

C language - Blue Bridge Cup - 7 segment code

队列管理器running状态下无法查看通道

Minecraft安装资源包
![[blackmail virus data recovery] suffix Crylock blackmail virus](/img/b2/8e3a65dd250b9194cfc175138c740c.jpg)
[blackmail virus data recovery] suffix Crylock blackmail virus
随机推荐
C4D quick start tutorial - C4d mapping
Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel
Gocv image reading and display
C language - Blue Bridge Cup - 7 segment code
libusb的使用
Gocv image cutting and display
[blackmail virus data recovery] suffix Crylock blackmail virus
Gocv split color channel
Benefits of ufcs of D
Function ‘ngram‘ is not defined
Linux binary installation Oracle database 19C
Nacos 下载启动、配置 MySQL 数据库
Oracle related statistics
Judge whether it is Sudoku
Webflux responsive programming
Function ‘ngram‘ is not defined
[blackmail virus data recovery] suffix Rook3 blackmail virus
AMQ6126问题解决思路
Use of libusb
Using recursive functions to solve the inverse problem of strings