给定一个由N 个整数组成的数组arr[]和一个数组Q[][] ,其中每一行都是{L, R}形式的查询。每个查询的任务是检查[L, R]范围内递增和递减子数组的计数是否相同。如果发现是真的,则打印“Yes” 。否则,打印“否” 。
例子:
Input: arr[] = {11, 13, 12, 14}, Q[][] = {{1, 4}, {2, 4}}
Output:
No
Yes
Explanation:
Query 1: There are two increasing subarrays {11, 13} and {12, 14} in the range [1, 4]. But there is only one decreasing subarray {13, 12} in the range [1, 4].
Therefore, print No.
Query 2: There is only one increasing subarray {12, 14} and one decreasing subarray {13, 12} in the range [2, 4].
Therefore, print Yes.
Input: arr[] = {16, 24, 32, 18, 14}, Q = {{1, 5}, {2, 3}, {2, 4}}
Output:
Yes
No
Yes
朴素的方法:解决这个问题的最简单的方法是从子数组{arr[L], arr[R]}生成所有可能的子数组,并检查递增和递减子数组的数量是否相同。如果发现是真的,则打印“是” 。否则,打印“否” 。
时间复杂度: O(Q*N 2 )
辅助空间: O(1)
有效的方法:为了优化上述方法,观察每个增加的子阵列后面跟着一个减少的子阵列,反之亦然,即它们交替增加或减少。因此,增加和减少的子数组的数量最多相差1 。因此,我们的想法是检查范围中的第一对和最后一对元素是否形成递增对。如果发现是真的,则打印“否” 。否则,打印“是” 。对每个查询执行上述步骤以获得O(1) 中的结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if given range
// have equal number of increasing
// as well as decreasing subarrays
void checkCount(int A[], int Q[][2],
int q)
{
// Traverse each query
for (int i = 0; i < q; i++) {
int L = Q[i][0];
int R = Q[i][1];
// For 0-based indexing
L--, R--;
// Condition for same count of
// increasing & decreasing subarray
if ((A[L] < A[L + 1])
!= (A[R - 1] < A[R])) {
cout << "Yes\n";
}
else {
cout << "No\n";
}
}
}
// Driver Code
int main()
{
int arr[] = { 11, 13, 12, 14 };
int Q[][2] = { { 1, 4 }, { 2, 4 } };
int q = sizeof(Q) / sizeof(Q[0]);
checkCount(arr, Q, q);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to check if given range
// have equal number of increasing
// as well as decreasing subarrays
static void checkCount(int A[], int Q[][],
int q)
{
// Traverse each query
for(int i = 0; i < q; i++)
{
int L = Q[i][0];
int R = Q[i][1];
// For 0-based indexing
L--;
R--;
// Condition for same count of
// increasing & decreasing subarray
if ((A[L] < A[L + 1]) !=
(A[R - 1] < A[R]))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 11, 13, 12, 14 };
int Q[][] = { { 1, 4 }, { 2, 4 } };
int q = Q.length;
checkCount(arr, Q, q);
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python3 program for the above approach
# Function to check if given range
# have equal number of increasing
# as well as decreasing subarrays
def checkCount(A, Q, q):
# Traverse each query
for i in range(q):
L = Q[i][0]
R = Q[i][1]
# For 0-based indexing
L -= 1
R -= 1
# Condition for same count of
# increasing & decreasing subarray
if ((A[L] < A[L + 1]) !=
(A[R - 1] < A[R])):
print("Yes")
else:
print("No")
# Driver Code
if __name__ == '__main__':
arr = [ 11, 13, 12, 14 ]
Q = [ [ 1, 4 ], [ 2, 4 ] ]
q = len(Q)
checkCount(arr, Q, q)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if given range
// have equal number of increasing
// as well as decreasing subarrays
static void checkCount(int[] A, int[,] Q,
int q)
{
// Traverse each query
for(int i = 0; i < q; i++)
{
int L = Q[i, 0];
int R = Q[i, 1];
// For 0-based indexing
L--;
R--;
// Condition for same count of
// increasing & decreasing subarray
if ((A[L] < A[L + 1]) !=
(A[R - 1] < A[R]))
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
}
}
// Driver Code
public static void Main()
{
int[] arr = { 11, 13, 12, 14 };
int[,] Q = { { 1, 4 }, { 2, 4 } };
int q = Q.GetLength(0);
checkCount(arr, Q, q);
}
}
// This code is contributed by code_hunt
Javascript
No
Yes
时间复杂度: O(Q)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。