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 ...
随机推荐
- hdu 3549 Flow Problem Edmonds_Karp算法求解最大流
Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp ...
- Monitor All SQL Queries in MySQL (alias mysql profiler)
video from youtube: http://www.youtube.com/watch?v=79NWqv3aPRI one blog post: Monitor All SQL Querie ...
- DB天气app冲刺第三天
昨天很郁闷而且烦躁的的过了一天 什么也没弄.今天其实也没有怎么做..进度非常慢.. 因为个人的问题 所以这两天的效率非常慢. 但今天还是做了一些东西.把listview做出来了.做出了一个按钮的效果. ...
- Memcache仅仅支持简单数据类型
Memcache仅仅支持简单数据类型 ,复杂数据类型需要应用自己处理 从数据库当中取出数据[User [id=1, username=guowuxin, password=guowuxin], Use ...
- YARN加载本地库抛出Unable to load native-hadoop library解决办法
YARN加载本地库抛出Unable to load native-hadoop library解决办法 用官方的Hadoop 2.1.0-beta安装后,每次hadoop命令进去都会抛出这样一个War ...
- 数组方法slice()把类数组转成数组和复制一个数组
function a(){ console.log(arguments.length); var c = [].slice.call(arguments);//类数组转成数组 c.push(5); c ...
- bzoj 1314: River过河 优先队列
1314: River过河 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 26 Solved: 10[Submit][Status][Discuss ...
- backbone案例
http://www.kuqin.com/webpagedesign/20120807/324101.html http://udonmai.com/code/todos-backbone%E6%A1 ...
- PIL(Python Image Library)生成验证码
# -*- coding: utf-8 -*-#导入三个模块import Image,ImageDraw,ImageFontimport randomimport math'''基本功能'''#图片宽 ...
- 【转】DCX (数独-八皇后问题)
还没学会,先转了再说.. 出处:http://grenet.cnblogs.com/ 跳跃的舞者,舞蹈链(Dancing Links)算法--求解精确覆盖问题 精确覆盖问题的定义:给定一个由0-1 ...