当前位置:网站首页>LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
2022-07-07 15:32:00 【@小红花】
问题描述
给你一个整数数组 arr 和一个整数值 target 。
请你在 arr 中找 两个互不重叠的子数组 且它们的和都等于 target 。可能会有多种方案,请你返回满足要求的两个子数组长度和的 最小值 。
请返回满足要求的最小长度和,如果无法找到这样的两个子数组,请返回 -1 。
示例 1:
输入:arr = [3,2,2,4,3], target = 3
输出:2
解释:只有两个子数组和为 3 ([3] 和 [3])。它们的长度和为 2 。
示例 2:输入:arr = [7,3,4,7], target = 7
输出:2
解释:尽管我们有 3 个互不重叠的子数组和为 7 ([7], [3,4] 和 [7]),但我们会选择第一个和第三个子数组,因为它们的长度和 2 是最小值。
示例 3:输入:arr = [4,3,2,6,2,3,4], target = 6
输出:-1
解释:我们只有一个和为 6 的子数组。
示例 4:输入:arr = [5,5,4,4,5], target = 3
输出:-1
解释:我们无法找到和为 3 的子数组。
示例 5:输入:arr = [3,1,1,1,5,1,2,1], target = 3
输出:3
解释:注意子数组 [1,2] 和 [2,1] 不能成为一个方案因为它们重叠了。
提示:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-two-non-overlapping-sub-arrays-each-with-target-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Java
class Solution {
public int minSumOfLengths(int[] arr, int target) {
int n = arr.length;
int[] dp = new int[n];
//要求最小,填充为较大的数
Arrays.fill(dp,Integer.MAX_VALUE / 2);
int ans = Integer.MAX_VALUE;
//双指针
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;
}
}边栏推荐
- pycharm 终端部启用虚拟环境
- Advanced C language -- function pointer
- The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
- 3000 words speak through HTTP cache
- [medical segmentation] attention Unet
- 谎牛计数(春季每日一题 53)
- 最新阿里P7技术体系,妈妈再也不用担心我找工作了
- SqlServer2014+: 创建表的同时创建索引
- Talk about the realization of authority control and transaction record function of SAP system
- Prediction - Grey Prediction
猜你喜欢

Three. JS series (2): API structure diagram-2

【DesignMode】代理模式(proxy pattern)

水平垂直居中 方法 和兼容
最新Android高级面试题汇总,Android面试题及答案

最新阿里P7技术体系,妈妈再也不用担心我找工作了

低代码(lowcode)帮助运输公司增强供应链管理的4种方式
![[C language] question set of X](/img/17/bfa57de183c44cf0a3c6637bb65a9d.jpg)
[C language] question set of X

Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions

记录Servlet学习时的一次乱码

"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
随机推荐
Binary search tree (basic operation)
time标准库
低代码(lowcode)帮助运输公司增强供应链管理的4种方式
打造All-in-One应用开发平台,轻流树立无代码行业标杆
What is the difference between IP address and physical address
【DesignMode】享元模式(Flyweight Pattern)
运算符
The differences between exit, exit (0), exit (1), exit ('0 '), exit ('1'), die and return in PHP
值得一看,面试考点与面试技巧
Inner monologue of accidental promotion
Pycharm terminal enables virtual environment
Arduino 控制的双足机器人
ORACLE进阶(六)ORACLE expdp/impdp详解
Ray and OBB intersection detection
作为Android开发程序员,android高级面试
编程模式-表驱动编程
Vs2019 configuration matrix library eigen
HAVE FUN | “飞船计划”活动最新进展
第九届 蓝桥杯 决赛 交换次数
射线与OBB相交检测