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 ‘#‘.

  Think for a while......

  Typical DP-solved question.

  

  1 /*************************************************
  2 Author:Zhou You
  3 Time:2014.09.09
  4 *************************************************/
  5 #include <iostream>
  6 #include <cstdio>
  7
  8 using namespace std;
  9
 10 struct matrix_element
 11 {
 12 public:
 13     matrix_element():
 14         star_num_(0),
 15         hash_num_(0){}
 16
 17     matrix_element(unsigned star_num,unsigned hash_num):
 18         star_num_(star_num),
 19         hash_num_(hash_num){
 20     }
 21
 22     unsigned star_num_;
 23     unsigned hash_num_;
 24 };
 25
 26 void BuildMatrix(matrix_element *** pmaze,unsigned row_num,unsigned column_num)
 27 {
 28     *pmaze = new matrix_element*[row_num];
 29     for(unsigned i=0;i<row_num;++i){
 30         (*pmaze)[i] = new matrix_element[column_num];
 31     }
 32 }
 33
 34 void ReleaseMatrix(matrix_element ***pmaze,unsigned row_num)
 35 {
 36     if(!pmaze) return;
 37
 38     for(unsigned i=0;i<row_num;++i){
 39         delete [](*pmaze)[i];
 40     }
 41
 42     delete [](*pmaze);
 43 }
 44
 45 void CoreSolve(char **parray,unsigned element_num)
 46 {
 47     matrix_element **pnote = NULL;
 48     BuildMatrix(&pnote,element_num,element_num);
 49
 50     for(unsigned i=0;i<element_num;++i){
 51         if((*parray)[i]==‘*‘){
 52             ++pnote[i][i].star_num_;
 53         }else if((*parray)[i]==‘#‘){
 54             ++pnote[i][i].hash_num_;
 55         }
 56     }
 57
 58     int index_start = -1,index_end = -1;
 59     unsigned cur_length = 0;
 60
 61     for(unsigned sub_array_length = 2;sub_array_length<=element_num;++sub_array_length){
 62         for(unsigned i=0;i<=element_num-sub_array_length;++i){
 63             pnote[i][i+sub_array_length-1].hash_num_ =
 64             pnote[i][i+sub_array_length-2].hash_num_+
 65             pnote[i+sub_array_length-1][i+sub_array_length-1].hash_num_;
 66
 67             pnote[i][i+sub_array_length-1].star_num_ =
 68             pnote[i][i+sub_array_length-2].star_num_+
 69             pnote[i+sub_array_length-1][i+sub_array_length-1].star_num_;
 70
 71             if(pnote[i][i+sub_array_length-1].star_num_==
 72                pnote[i][i+sub_array_length-1].hash_num_){
 73                 if(sub_array_length>cur_length){
 74                     cur_length = sub_array_length;
 75                     index_start = i;
 76                     index_end = i+sub_array_length-1;
 77                 }
 78             }
 79         }
 80     }
 81
 82     cout<<index_start<<" "<<index_end;
 83
 84     ReleaseMatrix(&pnote,element_num);
 85 }
 86
 87 void Solve()
 88 {
 89     unsigned element_num = 0;
 90     cin>>element_num;
 91
 92     char *parray = new char[element_num];
 93     for(unsigned i=0;i<element_num;++i){
 94         cin>>parray[i];
 95     }
 96
 97     CoreSolve(&parray,element_num);
 98     delete []parray;
 99 }
100
101 int main()
102 {
103     freopen("data.in","r",stdin);
104     freopen("data.out","w",stdout);
105
106     unsigned case_num = 0;
107     cin>>case_num;
108
109     for(unsigned i=1;i<=case_num;++i){
110         cout<<"Case #"<<i<<" ";
111         Solve();
112         cout<<endl;
113     }
114
115     return 0;
116 }

  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
时间: 2024-11-15 20:40:45

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 >= 3 There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1] (Note tha

[Swift]LeetCode845. 数组中的最长山脉 | Longest Mountain in Array

Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length >= 3 There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1] (Note tha

Longest Mountain in Array 数组中的最长山脉

我们把数组 A 中符合下列属性的任意连续子数组 B 称为 “山脉”: B.length >= 3 存在 0 < i < B.length - 1 使得 B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1](注意:B 可以是 A 的任意子数组,包括整个数组 A.) 给出一个整数数组 A,返回最长 “山脉” 的长度. 如果不含有 “山脉” 则返回 0. 示例 1: 输入:[2,1,

[LeetCode] Contiguous Array 邻近数组

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. Example 1: Input: [0,1] Output: 2 Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1. Example 2: Input: [0,1,0] Outp

525. Contiguous Array

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. Example 1: Input: [0,1] Output: 2 Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1. Example 2: Input: [0,1,0] Outp

[Swift]LeetCode525. 连续数组 | Contiguous Array

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. Example 1: Input: [0,1] Output: 2 Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1.  Example 2: Input: [0,1,0] Out

[leetcode-525-Contiguous Array]

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. Example 1: Input: [0,1] Output: 2 Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1. Example 2: Input: [0,1,0] Outp

leetcode[169] Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素. 我想到的方法是 1. 排序,然后扫描一次就知道了.总共nlgn 2. 哈希,记录每个次数,O(n)的时间和空间. class Solution { public: int majorityElement(vector<int> &num) { unordered_map<int, int> umap; for (int i = 0; i < num.size(); i++) { umap[num[i]

【leetcode 哈希表】Majority Element

[leetcode 哈希表]Majority Element @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is no