当前位置:网站首页>"Interview high frequency question" is 1.5/5 difficult, and the classic "prefix and + dichotomy" application question
"Interview high frequency question" is 1.5/5 difficult, and the classic "prefix and + dichotomy" application question
2022-07-02 09:07:00 【Programmer Qiqi】
Title Description
This is a LeetCode Upper 209. Subarray with the smallest length , The difficulty is secondary .
Tag : 「 The prefix and 」、「 Two points 」
Given a containing n An array of positive integers and a positive integer target.
Find the sum of the array ≥ target The smallest length of Continuous subarray [numsl,numsl+1,...,numsr−1,numsr][nums_l, nums_{l+1}, ..., nums_{r-1}, nums_r][numsl,numsl+1,...,numsr−1,numsr] , And return its length . If there is no sub array that meets the conditions , return 000 .
Example 1:
Input :target = 7, nums = [2,3,1,2,4,3]
Output :2
explain : Subarray [4,3] Is the smallest subarray in this condition .
Copy code Example 2:
Input :target = 4, nums = [1,4,4]
Output :1
Copy code Example 3:
Input :target = 11, nums = [1,1,1,1,1,1,1,1]
Output :0
Copy code Tips :
- 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
The prefix and + Two points
utilize nums[i]nums[i]nums[i] The data range of is [1,105][1, 10^5][1,105], It is known that the prefix and array satisfy 「 Monotone increasing 」.
Let's preprocess the prefix and array sum( Prefixes and array subscripts default from 111 Start ), For everyone nums[i]nums[i]nums[i] for , Assume that the corresponding prefix and value are s=sum[i+1]s = sum[i + 1]s=sum[i+1], We will nums[i]nums[i]nums[i] Treat as the right endpoint of the array , The question turned to : Subscript in prefix and array [0,i][0, i][0,i] Find satisfaction in the range Value less than or equal to s−ts - ts−t The maximum subscript of , Serve as the previous value of the left endpoint of the array .
Using prefixes and arrays 「 Monotone increasing 」( That is, it has two stages ), This operation can be used 「 Two points 」 To do it .
Code :
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;
}
}
Copy code - Time complexity : The complexity of preprocessing prefix and array is O(n)O(n)O(n), The complexity of traversing prefix and array statistics answer is O(nlogn)O(n\log{n})O(nlogn). The overall complexity is O(nlogn)O(n\log{n})O(nlogn)
- Spatial complexity :O(n)O(n)O(n)
Last
This is us. 「 Brush through LeetCode」 The first of the series No.209 piece , The series begins with 2021/01/01, As of the start date LeetCode I have in common 1916 questions , Part of it is a locked question , We will finish all the questions without lock first .
In this series , In addition to explaining the idea of solving problems , And give the most concise code possible . If the general solution is involved, there will be corresponding code templates
summary
Warehouse :github.com/SharingSour… .
In the warehouse address , You can see the links to the series 、 The corresponding code of the series 、LeetCode Links to the original problem and other preferred solutions .
边栏推荐
猜你喜欢

How to realize asynchronous programming in a synchronous way?

Openshift deployment application

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

Servlet全解:继承关系、生命周期、容器和请求转发与重定向等

聊聊消息队列高性能的秘密——零拷贝技术

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

以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化

Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`

Minecraft install resource pack

commands out of sync. did you run multiple statements at once
随机推荐
commands out of sync. did you run multiple statements at once
C call system sound beep~
Pyspark de duplication dropduplicates, distinct; withColumn、lit、col; unionByName、groupBy
Oracle 相关统计
[staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)
gocv图片读取并展示
C4D quick start tutorial - Chamfer
Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
2022/2/14 summary
Hengyuan cloud_ Can aiphacode replace programmers?
Qt的connect函数和disconnect函数
1、 QT's core class QObject
Minecraft air Island service
【Go实战基础】gin 如何自定义和使用一个中间件
C Gaode map obtains the address according to longitude and latitude
Minecraft install resource pack
2022/2/13 summary
C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
libusb的使用
Installing Oracle database 19C for Linux