[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. ...
随机推荐
- 在DBGrid中实现多选功能
1.首先把DBGrid->options-dgMulitSelect设为True. dgRowSelect也设为True,此属性设为true后,DBGrid将不能编辑,如何实现能否编辑代码如下 ...
- 第191天:js---Array常用属性和方法总结
Array---常用属性和方法总结 1.Array对象构造函数 /*Array对象构造函数*/ /*组合记忆 shift unshift pop push 添加和删除 shift unshift 从数 ...
- innerHTML与innerText的区别: 前者获取的是dom对象内的所有html元素 后者获取的是dom对象里面的纯文本元素
- java 父类的引用调用自己的属性 但是调用的方法必须是重写过的父类的方法 因为编译时候把他当作父类 运行时候才是他自己 所以必须重写父类得方法
- [九]SpringBoot 之 定时任务
代码: package me.shijunjie.config; import org.springframework.context.annotation.Configuration; import ...
- 洛谷 P2951 [USACO09OPEN]捉迷藏Hide and Seek
题目戳 题目描述 Bessie is playing hide and seek (a game in which a number of players hide and a single play ...
- 【组合数学】【P4996】 咕咕咕
Description 小 F 注意到,自己总是在某些情况下会产生歉意.每当他要检查自己的任务表来决定下一项任务的时候,如果当前他干了某些事情,但是没干另一些事情,那么他就会产生一定量的歉意--比如, ...
- window.location.hash在firefox下中文自动转码为UTF-8问题
1.window.location.hash window.location.hash这个属性主要是读取和写入网页位置的,我们经常会用来控制网页单页面跳转或者是控制网页位置.然而这个属性在firefo ...
- 微信小程序踩过的坑
之前用小程序开发工具做过一个项目了,最近又新开了一个项目,在登录的时候发现总是提示code不合法,找了半天也未发现原因 后来同事提醒是不是因为开发工具里设置的AppId的问题,果断将当前工具里默认Ap ...
- kendalltau肯德尔和谐系数
sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...