[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. ...
随机推荐
- web项目访问路径上为什么不能写上WebContent
我们常常在WEB项目中要写很多的访问路径,比如说/good/target.jsp;目录结构中从来不会带有项目目录结构的WebContent?这到底的为什么呢? 我们知道WEB项目是放在容器上运行的,而 ...
- Contest 8
A:做法应该很多,比较好想的是每个点都往上倍增找到其能更新到的点. #include<iostream> #include<cstdio> #include<cstdli ...
- VSS2005设置不输入密码直接登录VSS
1.登录管理员 2.Tools-->Options-->General -->Use network name for automatic user log in 去掉勾选不自动登 ...
- Java操作excel(POI)
由于在项目中使用了将excel数据导入到数据库.在这里分享一下. 这里使用的POI方式,支持两种格式(xls,xlsx) package com.entity; import java.io.File ...
- 【JavaScript】面向对象的程序设计
一.前言 接着上一篇的内容,继续JavaScript的学习. 二.内容 属性类型 //数据属性[Configurable] —— 能否通过delete删除属性从而重新定义属性,能否修改属 ...
- 【BZOJ1494】【NOI2007】生成树计数(动态规划,矩阵快速幂)
[BZOJ1494][NOI2007]生成树计数(动态规划,矩阵快速幂) 题面 Description 最近,小栋在无向连通图的生成树个数计算方面有了惊人的进展,他发现: ·n个结点的环的生成树个数为 ...
- SMBv3空指针引用dos漏洞复现
0x01 前言 去年年底,当设置一个模拟器来定位SMB协议时,发现了一个如此简单而又非常有效的攻击大型企业的漏洞.TL; DR:一个拒绝服务错误允许BSOD协议向Windows 8.1和Windows ...
- 代码收藏系列--mysql--创建数据库、数据表、函数、存储过程命令
创建mysql数据库 CREATE DATABASE IF NOT EXISTS `database_name` DEFAULT CHARSET utf8 COLLATE utf8_general_c ...
- ContestHunter#24-C 逃不掉的路
Description: 求无向图的必经边 思路:一眼题 将无向图缩成树,然后求两点树上距离 #include<iostream> #include<vector> #incl ...
- SpringMVC接收复杂集合对象(参数)代码示例
原文: https://www.jb51.net/article/128233.htm SpringMVC接收复杂集合对象(参数)代码示例 更新时间:2017年11月15日 09:18:15 作者 ...