当前位置:网站首页>"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 .
边栏推荐
- Leetcode sword finger offer brush questions - day 22
- 队列管理器running状态下无法查看通道
- Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
- 十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
- Openshift container platform community okd 4.10.0 deployment
- Right click menu of QT
- Minecraft plug-in service opening
- commands out of sync. did you run multiple statements at once
- MYSQL安装出现问题(The service already exists)
- Nacos download, start and configure MySQL database
猜你喜欢

Kubesphere virtualization KSV installation experience

Minecraft air Island service

Minecraft群組服開服

MYSQL安装出现问题(The service already exists)

There is a problem with MySQL installation (the service already exists)

Flink-使用流批一体API统计单词数量

Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度

Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers

QT -- how to set shadow effect in QWidget

History of Web Technology
随机推荐
Web技术发展史
Linux二进制安装Oracle Database 19c
选择排序和插入排序
First week of JS study
【Go实战基础】如何安装和使用 gin
Minecraft插件服开服
Servlet全解:继承关系、生命周期、容器和请求转发与重定向等
【Go实战基础】gin 如何自定义和使用一个中间件
Minecraft group service opening
[blackmail virus data recovery] suffix Hydra blackmail virus
C Baidu map, Gaode map, Google map (GPS) longitude and latitude conversion
C call system sound beep~
[blackmail virus data recovery] suffix Crylock blackmail virus
2022/2/13 summary
Don't spend money, spend an hour to build your own blog website
Using recursive functions to solve the inverse problem of strings
CSDN Q & A_ Evaluation
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
Webflux responsive programming
随笔:RGB图像颜色分离(附代码)