Find longest contiguous sub array
It's still an Amazon interview question.
Given an array containing only stars '*' and hashes '#' . Find longest contiguous sub array that will contain equal no. of stars '*' and hashes '#'.Output the index range if the longest contiguous sub array does exist or else output -1 and -1,which denote no corresponding contiguous sub array exist.
Think for a while......
Typical DP-solved question.Every time I scan the sub array,I can use something that has been done.Say when I want to scan [2,6] in the original array,I should know the information about [2,5] in advance.So we can scan the array by length.The code is below.Time:O(n3),Space:O(n2),(n denotes the length of the array).
/*************************************************
Author:Zhou You
Time:2014.09.09
*************************************************/
#include <iostream>
#include <cstdio> using namespace std; struct matrix_element
{
public:
matrix_element():
star_num_(),
hash_num_(){} matrix_element(unsigned star_num,unsigned hash_num):
star_num_(star_num),
hash_num_(hash_num){
} unsigned star_num_;
unsigned hash_num_;
}; void BuildMatrix(matrix_element *** pmaze,unsigned row_num,unsigned column_num)
{
*pmaze = new matrix_element*[row_num];
for(unsigned i=;i<row_num;++i){
(*pmaze)[i] = new matrix_element[column_num];
}
} void ReleaseMatrix(matrix_element ***pmaze,unsigned row_num)
{
if(!pmaze) return; for(unsigned i=;i<row_num;++i){
delete [](*pmaze)[i];
} delete [](*pmaze);
} void CoreSolve(char **parray,unsigned element_num)
{
matrix_element **pnote = NULL;
BuildMatrix(&pnote,element_num,element_num); for(unsigned i=;i<element_num;++i){
if((*parray)[i]=='*'){
++pnote[i][i].star_num_;
}else if((*parray)[i]=='#'){
++pnote[i][i].hash_num_;
}
} int index_start = -,index_end = -;
unsigned cur_length = ; for(unsigned sub_array_length = ;sub_array_length<=element_num;++sub_array_length){
for(unsigned i=;i<=element_num-sub_array_length;++i){
pnote[i][i+sub_array_length-].hash_num_ =
pnote[i][i+sub_array_length-].hash_num_+
pnote[i+sub_array_length-][i+sub_array_length-].hash_num_; pnote[i][i+sub_array_length-].star_num_ =
pnote[i][i+sub_array_length-].star_num_+
pnote[i+sub_array_length-][i+sub_array_length-].star_num_; if(pnote[i][i+sub_array_length-].star_num_==
pnote[i][i+sub_array_length-].hash_num_){
if(sub_array_length>cur_length){
cur_length = sub_array_length;
index_start = i;
index_end = i+sub_array_length-;
}
}
}
} cout<<index_start<<" "<<index_end; ReleaseMatrix(&pnote,element_num);
} void Solve()
{
unsigned element_num = ;
cin>>element_num; char *parray = new char[element_num];
for(unsigned i=;i<element_num;++i){
cin>>parray[i];
} CoreSolve(&parray,element_num);
delete []parray;
} int main()
{
freopen("data.in","r",stdin);
freopen("data.out","w",stdout); unsigned case_num = ;
cin>>case_num; for(unsigned i=;i<=case_num;++i){
cout<<"Case #"<<i<<" ";
Solve();
cout<<endl;
} return ;
}
Case in data.in file
5
4
*#*#
4
*#**
5
*****
6
*###**
12
****###*****
Output data in data.out file
Case #1 0 3
Case #2 0 1
Case #3 -1 -1
Case #4 0 5
Case #5 1 6
Find longest contiguous sub array的更多相关文章
- [LeetCode] Longest Mountain in Array 数组中最长的山
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length ...
- [Swift]LeetCode845. 数组中的最长山脉 | Longest Mountain in Array
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length ...
- LeetCode 845. Longest Mountain in Array
原题链接在这里:https://leetcode.com/problems/longest-mountain-in-array/ 题目: Let's call any (contiguous) sub ...
- 【LeetCode】845. Longest Mountain in Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双数组 参考资料 日期 题目地址:https://l ...
- 【leetcode】845. Longest Mountain in Array
题目如下: 解题思路:本题的关键是找出从升序到降序的转折点.开到升序和降序,有没有联想的常见的一个动态规划的经典案例--求最长递增子序列.对于数组中每一个元素的mountain length就是左边升 ...
- Longest Mountain in Array 数组中的最长山脉
我们把数组 A 中符合下列属性的任意连续子数组 B 称为 “山脉”: B.length >= 3 存在 0 < i < B.length - 1 使得 B[0] < B[1] ...
- [LeetCode] Contiguous Array 邻近数组
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
- [Swift]LeetCode525. 连续数组 | Contiguous Array
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
- 994.Contiguous Array 邻近数组
描述 Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and ...
随机推荐
- 让WPF的Popup不总置顶的解决方案
使用WPF的Popup的时候会发现有一个问题,它总是会置顶,只要Popup的StayOpen不设置为False,它就一直呆在最顶端,挡住其他的窗口. 解决方案是继承Popup重新定义控件PopupEx ...
- 【BZOJ】1088: [SCOI2005]扫雷Mine
1088: [SCOI2005]扫雷Mine Description 相 信大家都玩过扫雷的游戏.那是在一个n*m的矩阵里面有一些雷,要你根据一些信息找出雷来.万圣节到了,“余”人国流行起了一种简单的 ...
- linux exec函数家族
1.exec家族一共有六个函数,分别是: (1)int execl(const char *path, const char *arg, ......); (2)int execle(const ch ...
- C++ 输入输出文件流(ifstream&ofstream)
ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间; 在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,包括我们要认识的文件I/O, ...
- 【Hades】ades是一个开源库,基于JPA和Spring构建,通过减少开发工作量显著的改进了数据访问层的实现
几乎每个应用系统都需要通过访问数据来完成工作.要想使用领域设计方法,你就需要为实体类定义和构建资源库来实现领域对象的持久化.目前开发人员经常使用JPA来实现持久化库.JPA让持久化变得非常容易,但是仍 ...
- MVC-Html.ActionLink的几种写法
Html.ActionLink("linkText","actionName") Html.ActionLink("linkText",&q ...
- 计算器(console version)
题目描述 请用python编写一个计算器的控制台程序,支持加减乘除.乘方.括号.小数点,运算符优先级为括号>乘方>乘除>加减,同级别运算按照从左向右的顺序计算. 输入描述 数字包括& ...
- input标签文字点击变颜色
<input type="text" class="ser_input"value="从这里搜索(^_^)" onfocus=&quo ...
- 1010 [HNOI2008]玩具装箱toy
斜率优化dp: 推荐学习http://www.cnblogs.com/perseawe/archive/2012/05/12/bz1010.html 看着别人的题解自己学着推,终于理解了 #inclu ...
- 服务器部署_linuix下 一台nginx 多域名
近日朋友要我帮他调服务器, 一. 初步需求如下: 1. 一台服务器下要放三个应用,对应三个域名:www.aaa.com,www.bbb.com,www.ccc.com. 2. 其中后两个应用也要可以用 ...