Find maximum non contiguous subarray that respect a specific rule [closed]
-
04-11-2019 - |
문제
Given these two arrays:
[5, 3, 4, 1, 2]
[1, 3, 2, 4, 5]
Find the maximum subsequence in both arrays that the index of the elements are in a crescent order:
Example: [3, 4]
it's an answer because the indexes are in a crescent way in both arrays. (same as [1, 2]
). Therefore, the subsequence the answer [3, 4, 1]
is wrong, because the indexes are the crescent in the first array, but not on the second one.
The output of the program should be the length of the max non-contiguous subarray.
This is the code I wrote for solving this, but it only takes the first subarray, and I'm having difficulty to generate the other possibilities
vector<pair<int, double>> esq;
vector<pair<int, double>> dir;
// N is the size of esq and dir
// pair<int, double> where int is the key (show in the example array) and double is the value, used for sort previously.
int cont = 1;
for (int i = 0; i < N - 1; i++)
{
int cont_aux = 1;
pair<int, double> pivot = esq[i];
auto it_dir = find_if(dir.begin(), dir.end(), [&pivot](const pair<int, double> &p) { return p.first == pivot.first; });
int last_index = it_dir - dir.begin();
for (int j = 0; j < N; j++)
{
pair<int, double> actual = esq[j];
auto it = find_if(dir.begin(), dir.end(), [&actual](const pair<int, double> &p) { return p.first == actual.first; });
int pos = it - dir.begin();
if (pos >= last_index) {
last_index = pos;
cont_aux++;
}
}
cont = max(cont, cont_aux);
}
cout << cont << endl;
올바른 솔루션이 없습니다
제휴하지 않습니다 cs.stackexchange