[Description] Given an array with only '1' and '0', find a largest length sub-array which contains equal number of '1' and '0'. Return the largest number.

e.g. arr[] = {1,0,1,0,0,0,1,1,0,1,1,0,1,1,1,0,0,1,1,1,0,1,0,1,1}

  return : 16

[Thought] Preprocess this array by add 1/-1 to arr[i] while arr[i] is 1/0; find the last position of '0' as 'largest1'; find the farther distance of the same number as 'largest2'; return the larger one of 'largest1' and 'largest2'.  O(n^2)

[Implementation] C code:

 #include<stdio.h>

 int largestSubarray(int str[],int size)
{
int largest=;
int i,j;
str[]?(str[]=):(str[]=-);
// preprocess.
for(i=;i<size;i++)
{
str[i]?(str[i]=str[i-]+):(str[i]=str[i-]-);
}
// find the last 0's position, and find the farther distance of the same numbers.
for(i=;i<size;i++)
{
// find the last 0's position.
if( == str[i] && (i+)>largest)
{
largest=i+;
// printf("0:%d\n",largest);
}
// find the farther distance of the same numbers.
for(j=;j<i;j++)
{
if(str[i] == str[j] && (i-j)>largest)
{
largest=i-j;
// printf("other:%d\n",largest);
break;
}
}
}
return largest;
} int main()
{
int str[]={,,,,,,,,,,,,,,,,,,,,,,,,};
int size=sizeof(str)/sizeof(str[]);
int i; printf("Initianl Sequence which size is %d :\n",size);
for(i=;i<size;i++)
{
printf("%d, ",str[i]);
}
printf("\nlargest:%d\n",largestSubarray(str,size));
}

[003] largest_subarray_with_equal_1&0的更多相关文章

  1. 设置浮点数的显示精度&precision(0)

    /*    设置浮点数的显示精度    cout.precision(int)可以设置浮点数的显示精度(不包括小数点)        注: 1.如果设置的精度大于浮点数的位数,如果浮点数能根据IEEE ...

  2. jdbc工具类2..0

    一.创建外部文件 url=jdbc:mysql:///qy66 use=root password=root driver=com.mysql.jdbc.Driver 二.创建工具类 package ...

  3. 猜年龄v2.0

    ''' 用户登录,只有三次机会 给定年龄,用户可以猜三次年龄 年龄猜对,让用户选择两次奖励,输入无效字符,让其选择要不要礼物 用户选择两次奖励后可以退出,选择第一次后提示还有一次 ''' #基本信息定 ...

  4. 数据库中树形列表(以easyui的tree为例)

    构造一棵easyui前台框架的一个树形列表为例后台框架是spring MVC+JPA. 先看一下数据库是怎么建的,怎么存放的数据 下面是实体类 /** * 部门类 用户所属部门(这里的部门是一个相对抽 ...

  5. 1ms引发的问题

    最近在跟SQLServer数据库进行交互的时候发现一个奇怪的问题,在往数据库里边插入日期型数据的时候,在C#里面赋值的为 2014/05/19 23:59:59,但是存到数据库里边就变成了2014/0 ...

  6. Unity3D安卓打包参数配置与兼容性的关系分析

    前言 在使用Unity3D工程导出安卓安装包的时候,往往会遇到兼容性的问题,针对某些机型,要么无法打开游戏,要么会出现卡机的现象.面对这种情况,我们可以调节相关的参数来提高兼容性. 为了了解在打包时候 ...

  7. java提高篇(三)-----java的四舍五入

    Java小事非小事!!!!!!!!!!!! 四舍五入是我们小学的数学问题,这个问题对于我们程序猿来说就类似于1到10的加减乘除那么简单了.在讲解之间我们先看如下一个经典的案例: public stat ...

  8. [stm32] MPU6050 HMC5883 Kalman 融合算法移植

    一.卡尔曼滤波九轴融合算法stm32尝试 1.Kalman滤波文件[.h已经封装为结构体] /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronic ...

  9. squid日志配置与轮询

    squid日志分类及参数 SQUID默认的log文件非常多,其中最重要的LOG日志有三个,分别为access.log.store.log.cache.log.三个日志的记录的内容如下: access. ...

随机推荐

  1. Shell逐行读取文件的3种方法

    方法1:while循环中执行效率最高,最常用的方法. while read linedoecho $linedone  < filename 注释:这种方式在结束的时候需要执行文件,就好像是执行 ...

  2. 第136天:Web前端面试题总结(理论)

    Web前端面试题总结 HTML+CSS理论知识 1.讲讲输入完网址按下回车,到看到网页这个过程中发生了什么 a. 域名解析 b. 发起TCP的3次握手 c. 建立TCP连接后发起http请求 d. 服 ...

  3. LeetCode 696. Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  4. [十二]SpringBoot 之 servlet

    Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet.Filter.Listener.Interceptor 等等. 当使用spring-Boot时,嵌 ...

  5. [三]SpringBoot 之 热部署

    如下配置 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring ...

  6. 【BZOJ5319】军训列队(主席树)

    [BZOJ5319]军训列队(主席树) 题面 BZOJ 洛谷 题解 一眼题既视感... 首先很明显,每次询问的结果显然是做一次离散. 然后直接上主席树就好了... 查询答案的方式也很简单 考虑一下那个 ...

  7. 电子商务(电销)平台中商品模块(Product)数据库设计明细

    以下是自己在电子商务系统设计中的数据库设计经验总结,而今发表出来一起分享,如有不当,欢迎跟帖讨论~ 商品表 (product)|-- 自动编号 (product_id)|-- 商品名称 (produc ...

  8. [HEOI2014]逻辑翻译

    ywy_c_asm的良心题解 是道好题 体现了二进制位的形象递归思想,以及将FWT的思路(都是拆位分治)用到题目中的典范 可以暴力高斯消元.完全没有利用2^N以及+-1的良好性质 发现项数,方程和二进 ...

  9. Error: Chromium revision is not downloaded. Failed to download Chromium

    在使用prerender-spa-plugin做前端预渲染的时候,安装puppeteer的时候因为下载Chromium 失败报错,有如下解决方法: 1.使用Chromium 国内源 npm confi ...

  10. 形态学及其他集合运算(Morphological and Other Set Operations)

    摘    要:本实验主要实现形态学图像处理.主要验证图像集合的交并补运算.膨胀和腐蚀处理并利用图像集合的运算,实现形态学边界抽取算法并进行特征边界抽取.同时将膨胀和腐蚀扩展至灰度图像,编写函数实现灰度 ...