给定一个数组arr,该数组无序,但每个值均为正数,再给定一个正数k。求arr的所有子数组中所有元素相加和为k的最长子数组的长度
例如,arr = [1, 2, 1, 1, 1], k = 3
累加和为3的最长子数组为[1, 1, 1],所以结果返回3
[要求]
时间复杂度为,空间复杂度为
第一行两个整数N, k。N表示数组长度,k的定义已在题目描述中给出
第二行N个整数表示数组内的数
输出一个整数表示答案
5 3 1 2 1 1 1
3
#include <bits/stdc++.h> using namespace std; int main(){ int n, k; cin>>n>>k; int a[n]; for(int i=0;i<n;i++) cin>>a[i]; int Max=0, s=a[0]; for(int l=0,r=0;r<n;){ if(s==k){ Max = max(Max, r-l+1); s -= a[l++]; }else if(s<k){ r++; if(r==n) break; s += a[r]; }else s -= a[l++]; } cout<<Max<<endl; return 0; }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] params = br.readLine().split(" "); int n = Integer.parseInt(params[0]), k = Integer.parseInt(params[1]); params = br.readLine().split(" "); int[] arr = new int[n]; for(int i = 0; i < n; i++) arr[i] = Integer.parseInt(params[i]); int left = 0, right = 0, sum = 0, maxLen = 1; while(right < n){ sum += arr[right]; if(sum < k){ right ++; }else if(sum > k){ while(left < right && sum > k){ sum -= arr[left]; left ++; } if(sum == k) maxLen = Math.max(maxLen, right - left + 1); right ++; }else{ maxLen = Math.max(maxLen, right - left + 1); sum -= arr[left]; left ++; right ++; } } System.out.println(maxLen); } }
#include <bits/stdc++.h> using namespace std; int main(){ int n, k; cin>>n>>k; if(n<1 || k<0) return 0; vector<int> v(n, 0); for(int i=0;i<n;++i){ cin>>v[i]; } int left=0, right=0, sum=v[0], res=0; while(right < n){ if(sum == k){ res = max(res, right-left+1); sum -= v[left]; ++left; } else if(sum < k){ ++right; if(right == n) break; sum += v[right]; } else{ sum -= v[left]; ++left; } } cout<<res<<endl; return 0; }
#include<iostream> (720)#include<vector> #include<algorithm> using namespace std; int main(){ int n,k; cin>>n>>k; int inp[n]; for(int i = 0; i < n; i++){ scanf("%d", &inp[i]); } int left = 0, right = 0; int sum = inp[0]; int count = 0; while(right < n){ if(sum == k){ count = max(count,right - left + 1); sum -= inp[left]; left++; } else if(sum > k){ sum -= inp[left]; left++; } else if(sum < k){ right++; if(right >= n){ break; } else sum += inp[right]; } } cout<<count<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int n, k; cin >> n >> k; vector<int> arr(n); for(int i = 0; i < n; i ++) cin >> arr[i]; int Max = 0; int temp = k; int len = 0; int start=0, end = 0; while(end < n){ temp -= arr[end]; if(temp > 0) end++; else if(temp < 0){ temp += arr[start]; start++; temp += arr[end]; } else{ len = end - start + 1; Max = max(Max, len); temp += arr[start]; start++; end++; } } cout << Max << endl; }
#include <iostream> #include <vector> using namespace std; // 给定一个数组arr,该数组无序,但每个值均为正数,再给定一个正数k。求arr的所有子数组中所有元素相加和为k的最长子数组的长度 // 例如,arr = [1, 2, 1, 1, 1], k = 3 // 累加和为3的最长子数组为[1, 1, 1],所以结果返回3 // [要求] // 时间复杂度为O(n)O(n),空间复杂度为O(1)O(1) /** * 双指针法,通过sum变量和left,right来保持一个滑动窗口 */ int ksum(){ int n,k; cin>>n>>k; if(n==0){ return 0;//如果传入的n为0,直接返回0 } vector<int> nums; for (int i = 0; i < n; i++) { int tmp ; cin >> tmp; nums.push_back(tmp); } if(n==1){ return nums[0] == k ? 1 : 0;//如果只有一个数,比较这个数和k的大小即可 } int sum; int len = 0; int left = 0,right = 0; sum = nums[left]; while (left <= right && right < n-1)//循环结束条件是right到达倒数第二个元素 { if(sum == k){ len = max(len,right-left+1);//取较大值 right++; sum=sum+nums[right]-nums[left];//滑动窗口整体向右移动一个单位 left++; }else if(sum < k){ right++; sum+=nums[right];//滑动窗口右界向右移动一个单位 }else{ sum-=nums[left]; left++;//滑动窗口左届向右移动一个单位 } } return len; } int main(void){ cout << ksum() << endl; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++){ arr[i] = scanner.nextInt(); } int start = 0,end = 0,sum=0,maxLen = Integer.MIN_VALUE; while(start<n && end<n){ while(end<n){ sum += arr[end++]; if(sum==k){ maxLen = Math.max(maxLen,end-start); }else if(sum > k){ break; } } while(start < n){ sum -= arr[start++]; if(sum==k){ maxLen = Math.max(maxLen,end-start); }else if(sum < k){ break; } } } System.out.print(maxLen); } }
#include<bits/stdc++.h> using namespace std; int main() { int n,k; cin>>n>>k; vector<int>num(n); for(int i=0;i<n;i++) cin>>num[i]; int left = 0, right = 0, sum = num[0], len = 0; while (right<n){ if (sum < k){ right++; if (right == n) break; sum += num[right]; } else if (sum > k) sum -= num[left++]; else{ len = max(len, right - left + 1); sum -= num[left++]; } } cout<<len<<endl; return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int left = 0; int right = 0; int sum = arr[left]; int len = 0; while (right < n) { if (sum == k) { len = Math.max(right - left + 1, len); sum -= arr[left++]; } else if (sum > k) { sum -= arr[left++]; } else { right++; if (right == n) { break; } sum += arr[right]; } } System.out.println(len); } }
# include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> nums(n); for(int i = 0; i < n; ++i) cin >> nums[i]; int left = 0, right = 0; int sum = 0; int ans = 0; // 滑动窗口的最大窗口模板 while(right < n){ sum += nums[right]; while(sum > k){ sum -= nums[left]; left++; } // 多了这个 sum == k 的判断 if(sum == k) ans = max(ans, right - left + 1); right++; } cout << ans << endl; return 0; }
#include "bits/stdc++.h" using namespace std; int main() { int len; cin>>len; int target; cin>>target; vector<int> arr(len); int ret=0;//满足要求的最大长度 int cur=0;//当前长度 int i=0,j=0; int sum=0; for(int k=0;k<len;k++) { cin>>arr[k]; } while(i<len) { if(j>=len){sum-=arr[i];if(sum==target){ret=max(ret,len-i);}i++;continue;} else if(sum+arr[j]>target){sum-=arr[i];i++;} else if(sum+arr[j]==target){ret=max(ret,j-i+1);sum+=arr[j]; //sum-=arr[i];i++; j++;} else if(sum+arr[j]<target){sum+=arr[j];j++;} //cout<<sum<<' '; } cout<<ret; return 0; }
line1 = input() line2 = input() line1_ary = line1.split(' ') n = int(line1_ary[0]) k = int(line1_ary[1]) strs1 = line2.split(' ') def get_sum(left, right): result = 0 for i in range(left, right+1): result += int(strs1[i]) return result def test_max_length(strs, k): left = 0 right = 0 n = len(strs) sum = int(strs[0]) max_len = -1 while right < n: if sum == k: if right - left + 1 >max_len: max_len = right - left + 1 right += 1 if right < n: sum += int(strs1[right]) elif sum < k: right += 1 if right < n: sum += int(strs1[right]) else: if left + 1 > right: right += 1 if right < n: sum += int(strs1[right]) sum -= int(strs1[left]) left += 1 return max_len print(test_max_length(strs1, k))
import java.util.*; public class Main{ public static void main(String [] args){ Scanner input = new Scanner(System.in); int N,k; N = input.nextInt(); k = input.nextInt(); int [] arr = new int[N]; for(int i = 0;i < N;++i) arr[i] = input.nextInt(); int left = 0; /* 注意如果sum初始化为0,right的初始化从-1开始, 注意终止条件的判定,注意初始化条件。 */ int right = -1; int sum = 0; int maxlen = 0; while(right < N){ if(sum == k){ maxlen = Math.max(maxlen,right-left+1); sum -= arr[left]; ++left; }else if(sum < k){ ++right; if(right == N) break; sum += arr[right]; }else{ sum -= arr[left]; ++left; } } System.out.printf("%d\n",maxlen); } }
import java.util.Scanner; public class Main{ public static int getMaxLength(int[] arr,int k){ if(arr==null||arr.length==0||k<=0){ return 0; } int left=0; int right=0; int sum=arr[0]; int len=0; while (right<arr.length){ if(sum==k){ len=Math.max(len,right-left+1); sum-=arr[left++]; }else if(sum<k){ right++; if(right==arr.length){ break; } sum+=arr[right]; }else{ sum-=arr[left++]; } } return len; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = sc.nextInt(); } System.out.println(getMaxLength(arr,k)); } }