[003] largest_subarray_with_equal_1&0
[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的更多相关文章
- 设置浮点数的显示精度&precision(0)
/* 设置浮点数的显示精度 cout.precision(int)可以设置浮点数的显示精度(不包括小数点) 注: 1.如果设置的精度大于浮点数的位数,如果浮点数能根据IEEE ...
- jdbc工具类2..0
一.创建外部文件 url=jdbc:mysql:///qy66 use=root password=root driver=com.mysql.jdbc.Driver 二.创建工具类 package ...
- 猜年龄v2.0
''' 用户登录,只有三次机会 给定年龄,用户可以猜三次年龄 年龄猜对,让用户选择两次奖励,输入无效字符,让其选择要不要礼物 用户选择两次奖励后可以退出,选择第一次后提示还有一次 ''' #基本信息定 ...
- 数据库中树形列表(以easyui的tree为例)
构造一棵easyui前台框架的一个树形列表为例后台框架是spring MVC+JPA. 先看一下数据库是怎么建的,怎么存放的数据 下面是实体类 /** * 部门类 用户所属部门(这里的部门是一个相对抽 ...
- 1ms引发的问题
最近在跟SQLServer数据库进行交互的时候发现一个奇怪的问题,在往数据库里边插入日期型数据的时候,在C#里面赋值的为 2014/05/19 23:59:59,但是存到数据库里边就变成了2014/0 ...
- Unity3D安卓打包参数配置与兼容性的关系分析
前言 在使用Unity3D工程导出安卓安装包的时候,往往会遇到兼容性的问题,针对某些机型,要么无法打开游戏,要么会出现卡机的现象.面对这种情况,我们可以调节相关的参数来提高兼容性. 为了了解在打包时候 ...
- java提高篇(三)-----java的四舍五入
Java小事非小事!!!!!!!!!!!! 四舍五入是我们小学的数学问题,这个问题对于我们程序猿来说就类似于1到10的加减乘除那么简单了.在讲解之间我们先看如下一个经典的案例: public stat ...
- [stm32] MPU6050 HMC5883 Kalman 融合算法移植
一.卡尔曼滤波九轴融合算法stm32尝试 1.Kalman滤波文件[.h已经封装为结构体] /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronic ...
- squid日志配置与轮询
squid日志分类及参数 SQUID默认的log文件非常多,其中最重要的LOG日志有三个,分别为access.log.store.log.cache.log.三个日志的记录的内容如下: access. ...
随机推荐
- Jquery简单实现Datepicker
cshtml: <input type="text" id="purchaseDate" name="PurchaseDate" va ...
- 使用cookie保存用户登录信息
写入Cookie HttpCookie _cookie = new HttpCookie("User"); _cookie.Values.Add("UserName&qu ...
- Streaming Big Data: Storm, Spark and Samza--转载
原文地址:http://www.javacodegeeks.com/2015/02/streaming-big-data-storm-spark-samza.html There are a numb ...
- 【HLSDK系列】怎么增加一种新实体
你平常肯定接触到很多比如 info_player_start hostage info_target 之类的实体,这里就解释一下怎么创建一种新的实体. 首先建立一个新的 .h 文件(当然你写在现有的文 ...
- 【Java】SVN下载maven项目到eclipse之后,项目红叉,pom.xml出现Missing artifact fakepath:dubbo:jar:2.8.5等缺少jar包情况
刚入公司,从svn上把代码弄下来之后导入eclipse,一般是maven项目,往往项目都会有红叉.如果排除代码本身问题,一般是jar包没有. 鼠标点开pom.xml文件,在约束那里一般有红叉,鼠标放上 ...
- Omeed 线段树
目录 题面 题解 代码 题面 2.12 - - - 题解 大概还是挺妙的? 首先基础分和连击分互不干扰,所以可以分开统计. 基础分的统计比较简单,等于: \[A \sum_{i = l}^{r} p_ ...
- 解题:POI 2013 Taxis
题面 设当前位置为$pos$,那么可以发现在出租车总部左侧时,每辆车的贡献是$x[i]-(d-pos)$,而在右侧时只有$x[i]>=m-d$的车能够把人送到,那么首先我们要找出最小的满足$x[ ...
- 【agc017E】Jigsaw
Portal -->agc017 Description 给你\(n\)块积木,每块积木由三个矩形组成,中间的矩形最高高度为\(h\),左边的矩形高度为\(a_i\)离底边高度为\(c_i\), ...
- C++堆和栈详解(转)
一.预备知识—程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其 操 ...
- php开启压缩gzip
php服务的开启压缩,节省带宽 看是否开启压缩的网站 http://www.cnblogs.com/GaZeon/p/5421906.html 找到php.ini,修改下面的 ,重启php-fpm z ...