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

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

leetcode解题之416#Partition Equal Subset SumJava版. 对半分(0-1背包,动态规划)

leetcode解题之416#Partition Equal Subset SumJava版. 对半分(0-1背包,动态规划)

PLAN_HASH_VALUE - How equal (and stable?) are your execution plans - part 1

http://oracle-randolf.blogspot.tw/2009/07/planhashvalue-how-equal-and-stable-are.html     Oracle ...
  • loryliu
  • loryliu
  • 2017年02月14日 15:19
  • 225

Complie and Debug x264 in VS2010 with Intel Complier(Parallel Studio XE 2011) 2011-06-09 11:59:0

Complie and Debug x264 in VS2010 with Intel Complier(Parallel Studio XE 2011)   2011-06-09 11:59:09...

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
  • 709

错误:android studio ---> Error:(1, 0) Plugin with id 'com.android.application' not found.

在导入一个Android Studio的项目时报出这个错误:Error:(1, 0) Plugin with id 'com.android.application' not found. ...

(2017多校训练第五场)HDU - 6092 Rikka with Subset 0-1背包

题目链接:点击打开链接 题意不再赘述。 下面来讲解一下第一个样例是怎么推导出答案的。 2 3 1 1 1 1 第一个数字和最后一个数字肯定是1,不需要关注它们。 下面来看第二个数字(去掉首尾之后的第一...

OCP-1Z0-051 第2题 视图的WITH CHECK OPTION选项

View the Exhibit to examine the description for the SALES table. Which views can have all DML opera...

Android错误threadid=1: thread exiting with uncaught exception (group=0x416298c8)

Android错误threadid=1: thread exiting with uncaught exception (group=0x416298c8)在项目开发中测试时崩溃,错误如上 该错误的...

W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x419b4c50)

今天编程时候遇到一个问题:   W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x419b4c50)  ...

Parameter index out of range (1 > number of parameters, which is 0).] with r

遇到Parameter index out of range (1 > number of parameters, which is 0)怎么办? 2011年05月12日 星期四 23:45遇到了P...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Longest Subarray with Equal "1" and "0"
举报原因:
原因补充:

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