首页 > 试题广场 >

未排序数组中累加和为给定值的最长子数组系列问题补1

[编程题]未排序数组中累加和为给定值的最长子数组系列问题补1
  • 热度指数:4330 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个无序数组arr,其中元素可正、可负、可0。求arr所有子数组中正数与负数个数相等的最长子数组的长度。
[要求]
时间复杂度为,空间复杂度为

输入描述:
第一行一个整数N,表示数组长度
接下来一行有N个数表示数组中的数


输出描述:
输出一个整数表示答案
示例1

输入

5
1 -2 1 1 1

输出

2

备注:

头像 Kagari22
发表于 2024-04-04 20:04:37
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 100001 int max(int a, int b) { return a > b ? a : b 展开全文
头像 牛客396959330号
发表于 2024-12-11 20:37:22
#include <iostream> #include <unordered_map> using namespace std; int n,presum,a[100005],st,ma; unordered_map<int,int>hmap; int main 展开全文
头像 哈哈~柳暗花明
发表于 2020-07-29 13:59:10
同上一题,正数看作1,负数看作-1,k=0m存储前i项和对应下标,a存储前i项和,ans最大长,即求第i项时m[a-k]是否存在 def solve(l, n): m = {0:-1} a = ans = 0 for i in range(n): if l[i 展开全文
头像 瓜瓜请多指教
发表于 2020-07-18 16:18:21
include <bits/stdc++.h> using namespace std; int main(){ int N,t,len=0; cin>>N; vector<int> arr(N+1,0); for(int i=1;i< 展开全文
头像 牛客695415901号
发表于 2024-04-12 00:18:14
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new S 展开全文