当前位置:网站首页>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;
}
}边栏推荐
- LeetCode 1626. 无矛盾的最佳球队 每日一题
- Lie cow count (spring daily question 53)
- Inner monologue of accidental promotion
- Spark Tuning (III): persistence reduces secondary queries
- JS中null NaN undefined这三个值有什么区别
- Personal notes of graphics (4)
- 无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
- 【DesignMode】享元模式(Flyweight Pattern)
- C语言进阶——函数指针
- As an Android Developer programmer, Android advanced interview
猜你喜欢

低代码(lowcode)帮助运输公司增强供应链管理的4种方式

Cesium(3):ThirdParty/zip. js
![[C language] question set of X](/img/17/bfa57de183c44cf0a3c6637bb65a9d.jpg)
[C language] question set of X

Temperature sensor chip used in temperature detector

The latest interview experience of Android manufacturers in 2022, Android view+handler+binder

谈谈 SAP 系统的权限管控和事务记录功能的实现
3000 words speak through HTTP cache
直接上干货,100%好评

掌握这套精编Android高级面试题解析,oppoAndroid面试题

记录Servlet学习时的一次乱码
随机推荐
Three. JS series (2): API structure diagram-2
Laravel changed the session from file saving to database saving
Imitate the choice of enterprise wechat conference room
掌握这个提升路径,面试资料分享
[designmode] proxy pattern
LeetCode 1981. 最小化目标值与所选元素的差 每日一题
Module VI
Ray and OBB intersection detection
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
Opencv configuration 2019vs
node:504报错
three. JS create cool snow effect
Personal notes of graphics (2)
LeetCode 1774. 最接近目标价格的甜点成本 每日一题
AutoLISP series (3): function function 3
应用在温度检测仪中的温度传感芯片
3000 words speak through HTTP cache
【Vulnhub靶场】THALES:1
SqlServer2014+: 创建表的同时创建索引
Sort out several important Android knowledge and advanced Android development interview questions