当前位置:网站首页>LeetCode 1477. Find two subarrays with sum as the target value and no overlap
LeetCode 1477. Find two subarrays with sum as the target value and no overlap
2022-07-07 16:59:00 【@Little safflower】
Problem description
Give you an array of integers arr And an integer value target .
Please come in arr In looking for Two non overlapping subarrays And their sum is equal to target . There may be many options , Please return the sum of the length of the two sub arrays that meet the requirements minimum value .
Please return the minimum length and , If you can't find such two sub arrays , Please return -1 .
Example 1:
Input :arr = [3,2,2,4,3], target = 3
Output :2
explain : There are only two subarrays and 3 ([3] and [3]). Their length and are 2 .
Example 2:Input :arr = [7,3,4,7], target = 7
Output :2
explain : Even though we have 3 The sum of two non overlapping subarrays is 7 ([7], [3,4] and [7]), But we will choose the first and third sub arrays , Because of their length and 2 Is the minimum .
Example 3:Input :arr = [4,3,2,6,2,3,4], target = 6
Output :-1
explain : We have only one sum for 6 Subarray .
Example 4:Input :arr = [5,5,4,4,5], target = 3
Output :-1
explain : We can't find and for 3 Subarray .
Example 5:Input :arr = [3,1,1,1,5,1,2,1], target = 3
Output :3
explain : Notice the subarray [1,2] and [2,1] Can't be a solution because they overlap .
Tips :
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8source : Power button (LeetCode)
link :https://leetcode.cn/problems/find-two-non-overlapping-sub-arrays-each-with-target-sum
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Java
class Solution {
public int minSumOfLengths(int[] arr, int target) {
int n = arr.length;
int[] dp = new int[n];
// Minimum requirements , Fill with a larger number
Arrays.fill(dp,Integer.MAX_VALUE / 2);
int ans = Integer.MAX_VALUE;
// Double pointer
int left = 0;
int sum = 0;
for(int right = 0;right < n;right++){
sum += arr[right];
while(left <= right && sum > target){
sum -= arr[left++];
}
if(sum == target){
dp[right] = right - left + 1;
if(left != 0){
ans = Math.min(ans,dp[left - 1] + dp[right]);
}
}
if(right != 0){
dp[right] = Math.min(dp[right - 1],dp[right]);
}
}
return ans > n ? -1 : ans;
}
}
边栏推荐
- Deep listening array deep listening watch
- C语言进阶——函数指针
- 【Seaborn】组合图表:PairPlot和JointPlot
- ATM系统
- LeetCode 403. 青蛙过河 每日一题
- 掌握这个提升路径,面试资料分享
- three. JS create cool snow effect
- Talk about the realization of authority control and transaction record function of SAP system
- Prediction - Grey Prediction
- SqlServer2014+: 创建表的同时创建索引
猜你喜欢
QML初学
作为Android开发程序员,android高级面试
Sort out several important Android knowledge and advanced Android development interview questions
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
低代码(lowcode)帮助运输公司增强供应链管理的4种方式
掌握这套精编Android高级面试题解析,oppoAndroid面试题
"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
skimage学习(3)——Gamma 和 log对比度调整、直方图均衡、为灰度图像着色
Personal notes of graphics (2)
DNS 系列(一):为什么更新了 DNS 记录不生效?
随机推荐
[C language] question set of X
射线与OBB相交检测
爬虫(17) - 面试(2) | 爬虫面试题库
skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配
LeetCode 1043. 分隔数组以得到最大和 每日一题
A tour of gRPC:03 - proto序列化/反序列化
QML初学
AutoLISP series (2): function function 2
LeetCode 300. 最长递增子序列 每日一题
浅浅理解.net core的路由
null == undefined
Ray and OBB intersection detection
23. 合并K个升序链表-c语言
QML beginner
LeetCode 1626. 无矛盾的最佳球队 每日一题
Advanced C language -- function pointer
【Seaborn】组合图表:PairPlot和JointPlot
【DesignMode】代理模式(proxy pattern)
Sqlserver2014+: create indexes while creating tables
AutoLISP series (1): function function 1