检查数组 B 中的每一对是否遵循与它们在 A 中的对应值相同的关系
给定两个大小为N的数组A[]和B[] ,任务是根据以下条件检查给定数组是否有效:
- A中索引i处的每个元素将仅与B中相同索引处的元素映射,即( A[i] 只能与 B[i] 映射)
- 对于 A 中的任意对 (A[i], A[j]),如果 A[i] > A[j] ,那么它在 B 中的对应值也应该更大,即B[i] > B[j] 应该是真的。
- 对于 A 中的任意对 (A[i], A[j]),如果 A[i] = A[j] ,那么它在 B 中的对应值也应该相等,即B[i] = B[j] 应该是真的。
例子:
Input: N = 3, A[ ] = {10, 1, 17}, B[ ] = {10, 5, 15}
Output: true
Explanation: Consider all pairs in array A:
=> (10 and 1): Since 10>1, and their values in B (10 and 5 respectively) follow the same relation, therefore this is a valid pair.
=> (1 and 17): Since 1<17, and their values in B (5 and 15 respectively) follow the same relation, therefore this is a valid pair.
=> (10 and 17): Since 10<17, and their values in B (10 and 15 respectively) follow the same relation, therefore this is a valid pair.
As all the pairs are valid, therefore the given arrays are also valid. Hence the output is true.
Input: N = 5, A[ ] = {8, 5, 5, 10, 15}, B[ ] = {50, 10, 10, 15, 5 }
Output: false
朴素方法:解决问题的最基本方法是找到数组 A 中的每一对,并检查数组 B 中的对应值是否满足该对之间的关系。如果存在任何这样的对,其中的值不满足,然后返回假。否则返回真。
时间复杂度: O(N 2 )
辅助空间: O(1)
有效的方法:
直觉:
The idea for this approach is based on the observation that if the elements in an Array are sorted in ascending order,
- Then the first element will be always smaller than or equal to the second element
- Similarly, the first element will also be smaller than or equal to the last element
- Hence any element at index i will be smaller than or equal to element at index j, if (i < j)
基于以上观察:
- If we try to sort the array A by remembering their corresponding values in array B, then instead of checking every pair in A, we can simply check for adjacent pairs in A to follow the conditions given in the problem.
- If all adjacent pairs in sorted A follows the conditions to be valid, then the given Arrays will be valid.
插图:
Suppose A[ ] = {10, 1, 17}, and B[ ] = {10, 5, 15}
If we sort A, by remembering their corresponding values in B, we get A[] = {1, 10, 17}, B[] = {5, 10, 15}
Now if we check adjacent pairs in A to follow the conditions given in problem, we get:
- Pair (1, 10): Since 1<10 and their values in B (5, 10) also follow same relation. Therefore this is a valid pair.
- Pair (10, 17): Since 10<17 and their values in B (10, 15) also follow same relation. Therefore this is a valid pair.
Since all the values in A has been verified, therefore the given arrays are also valid.
算法:按照以下步骤实现上述方法:
- 创建一个新的对向量,以 {A[i], B[i]} 格式存储相应的值。
- 根据数组 A 的值对向量进行排序。
- 对于向量中的每个相邻对,检查是否:
- 如果A[i] < A[i+1]和B[i] > B[i+1] ,那么这不是一个有效的对。
- 因此返回false。
- 如果A[i] == A[i+1]和B[i] != B[i+1] ,那么这不是一个有效的对。
- 因此返回false。
- 如果A[i] < A[i+1]和B[i] > B[i+1] ,那么这不是一个有效的对。
- 如果上述迭代中没有任何对满足无效对条件
- 返回真。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check the given array relations are valid or not
bool isValidArrays(int A[], int B[], int n)
{
// creating new vector of pair to store the array
// relations.
vector > v1;
// push elements of A with its corresponding
// value in B;
for (int i = 0; i < n; i++) {
v1.push_back(make_pair(A[i], B[i]));
}
// sort vector by first value
sort(v1.begin(), v1.end());
// check the given relations.
for (int i = 0; i < v1.size() - 1; i++) {
if (v1[i].first == v1[i + 1].first) {
// if relation is not valid return false
if (v1[i].second != v1[i + 1].second) {
return false;
}
}
else {
// if relation is not valid return false
if (v1[i].second >= v1[i + 1].second) {
return false;
}
}
}
return true;
}
// Driver Code
int main()
{
int A[] = { 10, 1, 17 };
int B[] = { 10, 5, 15 };
int N = sizeof(A) / sizeof(A[0]);
cout << boolalpha << isValidArrays(A, B, N);
return 0;
}
true
时间复杂度: O(N * log N)
辅助空间: O(N),用于创建对向量。