当前位置:网站首页>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;
}
}边栏推荐
- 最新高频Android面试题目分享,带你一起探究Android事件分发机制
- 【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid
- Sqlserver2014+: create indexes while creating tables
- 深度监听 数组深度监听 watch
- Build an all in one application development platform, light flow, and establish a code free industry benchmark
- [designmode] facade patterns
- Pycharm IDE下载
- 最新Android面试合集,android视频提取音频
- SqlServer2014+: 创建表的同时创建索引
- QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
猜你喜欢

Interface oriented programming

记录Servlet学习时的一次乱码

【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid

Imitate the choice of enterprise wechat conference room

skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配

Advanced C language -- function pointer

The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars
直接上干货,100%好评

如何选择合适的自动化测试工具?

Pychart ide Download
随机推荐
LeetCode 1986. 完成任务的最少工作时间段 每日一题
Opencv configuration 2019vs
Three. JS series (2): API structure diagram-2
字节跳动高工面试,轻松入门flutter
QML初学
[designmode] flyweight pattern
Direct dry goods, 100% praise
typescript ts基础知识之tsconfig.json配置选项
谎牛计数(春季每日一题 53)
Sort out several important Android knowledge and advanced Android development interview questions
[designmode] proxy pattern
Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images
面试题 01.02. 判定是否互为字符重排-辅助数组算法
作为Android开发程序员,android高级面试
Pisa-Proxy SQL 解析之 Lex & Yacc
Three. JS series (1): API structure diagram-1
QT 图片背景色像素处理法
在哪个期货公司开期货户最安全?
最新Android面试合集,android视频提取音频
Personal notes of graphics (4)