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 ...
随机推荐
- 大坑!常被忽视又不得不注意的小细节——%I64,%lld与cout(转载)
原地址:http://blog.csdn.net/thunders01/article/details/38879553 刚刚被坑完,OI一年了才知道%I64和%lld有区别(做题会不会太少),lon ...
- 关于《一步步学习ASP.NET MVC3》系列发布时间的说明
在写这个系列的时候,老魏也是下了很大的决心,因为平时基本上没有时间写文章,这回我要挑战我自己的意志力,决定要把这个系列写完整. 再次呢,老魏不能向大家保证什么时间结束,但基本上要保持一天一篇的进度,如 ...
- UIScrollView解决touchesBegan等方法不能触发的解方案
新建一个类继承自UIScrollView 并重写下面的方法 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [su ...
- Junit4.12、Hamcrest1.3、Eclemma的安装和使用
1. Junit4.12和Hamcrest1.3的安装过程 步骤: 网上下载Junit和Hamcrest包文件,保存在本地. 新建Java项目命名为Triangle,在Eclipse菜单栏选择项目(P ...
- 在asp.net中如何自己编写highcharts图表导出到自己的服务器上来
1.准备工作:网上下载highcharts导出的关键dll. 1).Svg.dll:因为highcharts的格式其实就是一个xml,采用svg的方式画图: 2).itextsha ...
- 当页面编辑或运行提交时,出现“从客户端中检测到有潜在危险的request.form值”问题,该怎么办呢?
最近在学习highcharts时,关于其中的导出功能,本来是想把导出的图片存放在本地,发现只有在电脑联网的情况下才可以一下导出图片,后来查阅了一番资料,才发现highcharts中的导出默认的官网服务 ...
- Temporary Post Used For Theme Detection (da655c32-bc15-41ad-bf89-e76c1ec1bea7 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
This is a temporary post that was not deleted. Please delete this manually. (997facfe-d420-4437-a222 ...
- 【弱省胡策】Round #5 Handle 解题报告
这个题是我出的 sb 题. 首先,我们可以得到: $$A_i = \sum_{j=i}^{n}{j\choose i}(-1)^{i+j}B_j$$ 我们先假设是对的,然后我们把这个关系带进来,有: ...
- Design Tutorial: Inverse the Problem
Codeforces Round #270 D:http://codeforces.com/contest/472/problem/D 题意:给以一张图,用邻接矩阵表示,现在问你这张图能不能够是一棵树 ...
- 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问
检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问 已重装office2 ...