Longest Subarray with Equal "1" and "0"

原创 2012年03月21日 10:37:20

Problem: Given an array that only contains "1" and "0", find the longest subarray which contains equal number of "1" and "0".

Solution: With hash table, we can have a O(N) solution. The detail is as follow:

  • First convert all "0" to "-1", then calculate c[i] = sum(a[0], ... , a[i]). It takes O(N) to calculate all the c[i].
  • Then our task is to find a c[i] and a c[j] such that  c[i] = c[j]  and |j-i| is maximum. With a hash table, we can finish this job by doing a linear scan with a time complexity of   O(N).  // 用hashtable存储具有相同key的position值
  • There is a special case you need to handle. When c[N-1] = 0 (assume N is the size of a), the longest subarray is just a itself.

Idea: For every sum value record its leftmost occrrence in the left array and rightmost occrnce in the right array.such that in sum value at ith index == sum value at jth index it means that the subarray from index i to j has = no. of 1's and 0's.

Code:
#include<stdio.h>
#include<conio.h>
int main()
{
    int arr[100], rite[200],lft[200],len[200],sum[100], n,i,tmp, lnth=0;
    scanf("%d",&n);
    printf("%d\n",n);
    scanf("%d",&tmp);
    if(tmp==0)
    sum[0] = -1;
    else
    sum[0] = tmp;
    for(i=1;i<n;i++)
    {
                     scanf("%d",&tmp);
                     arr[i] = tmp;
                     if(tmp==0)
                     sum[i] = sum[i-1] - 1; 
                     else
                     sum[i] = sum[i-1] + 1;
                     if(sum[i]==0)
                     lnth = i;
                    // printf("%d\n",sum[i]);
                     }
   //printf("sum = %d\n",sum[n-1]);                  
   for(i=0;i<(2*n-1);i++)
   {
                         lft[i] = 0;
                         rite[i] = 0;
                         }                  
   for(i=0;i<n;i++)
   {
                   if(sum[i]>0)    
                   rite[sum[i]+n-1] = i;
                   else if(sum[i]<0)
                   rite[sum[i]+n] = i;
                   }
   for(i=n-1;i>=0;i--)
   {
                   if(sum[i]>0)    
                   lft[sum[i]+n-1] = i;
                   else if(sum[i]<0)
                   lft[sum[i]+n] = i;
                      }
   for(i=0;i<(2*n-1);i++)
   {
                         if(lnth<(rite[i]-lft[i]))
                         lnth = (rite[i]-lft[i]);
                         }
    printf("length = %d",lnth);
    getch();
    return 0;
}


Ref:

http://tristan-interview.blogspot.com/2012/01/longest-subarray-with-equal-1-and-0.html

http://www.thealgorithmist.com/showthread.php/146-Array-of-0-s-and-1-s-Find-max-subarray-of-0-s-1-s

版权声明:本文为博主原创文章,未经博主允许不得转载。

Split array into two equal sum subArray

@(leetcode)[数组|Google]ProblemA zero-indexed array A consisting of N integers is given. An equilibriu...
  • lqcsp
  • lqcsp
  • 2016年08月26日 18:02
  • 952

tensorflow ValueError: Dimensions must be equal

在使用tf.matmul进行矩阵相乘的时候报了这么个错。。。。 File "/home/batman/.local/lib/python3.5/site-packages/tensorflow/p...
  • banjuanshu
  • banjuanshu
  • 2017年05月24日 22:56
  • 1645

LintCode Subarray Sum 子数组之和

给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置。样例 给出[-3, 1, 2, -3, 4],返回[0, 2] 或者 [1, 3].Given an in...
  • wutingyehe
  • wutingyehe
  • 2015年06月26日 13:24
  • 1475

ValueError: Shape must be rank 0 but is rank 1 for 'train_data/ReadFile' (op: 'ReadFile') with input

使用函数tf.train.slice_input_producer读取文件时,input_queue = tf.train.slice_input_producer( [fli...
  • u011961856
  • u011961856
  • 2017年08月17日 17:07
  • 1395

LeetCode(5) Longest Palindromic Substring

如果一个字符串从左向右写和从右向左写是一样的,这样的字符串就叫做palindromic string,如aba,或者abba。给定输入一个字符串,要输出一个子串,使得子串是输入字符串的最长的padro...
  • feliciafay
  • feliciafay
  • 2013年11月27日 14:00
  • 17826

523. Continuous Subarray Sum

LeetCode 题目地址:https://leetcode.com/problems/continuous-subarray-sum/#/description 问题分析&解决思路:问给一个非负的数...
  • richlaji
  • richlaji
  • 2017年04月03日 17:06
  • 533

【leetcode】【53】Maximum Subarray

一、问题描述 Find the contiguous subarray within an array (containing at least one number) which has ...
  • xiaoliucool1314
  • xiaoliucool1314
  • 2016年03月01日 11:21
  • 356

53. Maximum Subarray-动态规划/分治

题: Find the contiguous subarray within an array (containing at least one number) which has the la...
  • u011567017
  • u011567017
  • 2016年09月04日 17:30
  • 267

longest consecutive subarray

leetcode 128 是 longest  consecutive sequence [2, 9, 4,3] 如果是longest  consecutive sequence 就是 [2, 4,...
  • binling
  • binling
  • 2015年06月30日 15:48
  • 283

longest continuous increasing subarray

第一题给一个int array, 找其中最长连续递增sub array的长度。比如[1, 2, 3, 1, 4],结果就应该是[1, 2, 3]的长度,为3。Two pointer public ...
  • sjphiChina
  • sjphiChina
  • 2016年08月02日 11:37
  • 91
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Longest Subarray with Equal "1" and "0"
举报原因:
原因补充:

(最多只允许输入30个字)