给定一个包含N 个元素的数组arr[]和形式为[X] 的Q 个查询。对于每个查询,任务是找到数组的最大区间[L, R] ,使得区间中的最大元素是arr[X] ,使得1 ≤ L ≤ X ≤ R 。
注意:该数组具有从 1 开始的索引。
例子:
Input: N = 5, arr[] = {2, 1, 2, 3, 2}, Q = 3, query[] = {1, 2, 4}
Output:
[1, 3]
[2, 2]
[1, 5]
Explanation :
In 1st query, x = 1, so arr[x] = 2 and answer is L = 1 and R = 3. here, we can see that max(arr[1], arr[2], arr[3]) = arr[x], which is the maximum intervals.
In 2nd query, x = 2, so arr[x] = 1 and since it is the smallest element of the array, so the interval contains only one element, thus the range is [2, 2].
In 3rd query, x = 4, so arr[x] = 4, which is maximum element of the arr[], so the answer is whole array, L = 1 and R = N.
Input: N = 4, arr[] = { 1, 2, 2, 4}, Q = 2, query[] = {1, 2}
Output:
[1, 1]
[1, 3]
Explanation:
In 1st query, x = 1, so arr[x] = 1 and since it is the smallest element of the array, so the interval contains only one element, thus the range is [1, 1].
In 2nd query, x = 2, so arr[x] = 2 and answer is L = 1 and R = 3. here, we can see that max(arr[1], arr[2], arr[3]) = arr[x] = arr[2] = 2, which is the maximum intervals.
方法:想法是预先计算arr[] 中从1 到 N 的每个值K的最大间隔。以下是步骤:
- 对于arr[] 中的每个元素K ,固定元素K的索引,然后找到我们可以将间隔扩展到它的左右。
- 减少左迭代器直到arr[left] ≤ K并类似地增加右迭代器直到arr[right] ≤ K 。
- left 和 right 的最终值表示区间的开始和结束索引,分别存储在arrL[]和arrR[] 中。
- 在我们为每个值预先计算间隔范围之后。然后,对于每个查询,我们需要打印arr[x]的区间范围,即arrL[arr[x]]和arrR[arr[x]] 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to precompute the interval
// for each query
void utilLargestInterval(int arr[],
int arrL[],
int arrR[],
int N)
{
// For every values [1, N] find
// the longest intervals
for (int maxValue = 1;
maxValue <= N; maxValue++) {
int lastIndex = 0;
// Iterate the array arr[]
for (int i = 1; i <= N; i++) {
if (lastIndex >= i
|| arr[i] != maxValue)
continue;
int left = i, right = i;
// Shift the left pointers
while (left > 0
&& arr[left] <= maxValue)
left--;
// Shift the right pointers
while (right <= N
&& arr[right] <= maxValue)
right++;
left++, right--;
lastIndex = right;
// Store the range of interval
// in arrL[] and arrR[].
for (int j = left; j <= right; j++) {
if (arr[j] == maxValue) {
arrL[j] = left;
arrR[j] = right;
}
}
}
}
}
// Function to find the largest interval
// for each query in Q[]
void largestInterval(
int arr[], int query[], int N, int Q)
{
// To store the L and R of X
int arrL[N + 1], arrR[N + 1];
// Function Call
utilLargestInterval(arr, arrL,
arrR, N);
// Iterate to find ranges for each query
for (int i = 0; i < Q; i++) {
cout << "[" << arrL[query[i]]
<< ", " << arrR[query[i]]
<< "]\n";
}
}
// Driver Code
int main()
{
int N = 5, Q = 3;
// Given array arr[]
int arr[N + 1] = { 0, 2, 1, 2, 3, 2 };
// Given Queries
int query[Q] = { 1, 2, 4 };
// Function Call
largestInterval(arr, query, N, Q);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to precompute the interval
// for each query
static void utilLargestInterval(int arr[],
int arrL[],
int arrR[],
int N)
{
// For every values [1, N] find
// the longest intervals
for(int maxValue = 1;
maxValue <= N; maxValue++)
{
int lastIndex = 0;
// Iterate the array arr[]
for(int i = 1; i <= N; i++)
{
if (lastIndex >= i ||
arr[i] != maxValue)
continue;
int left = i, right = i;
// Shift the left pointers
while (left > 0 &&
arr[left] <= maxValue)
left--;
// Shift the right pointers
while (right <= N &&
arr[right] <= maxValue)
right++;
left++;
right--;
lastIndex = right;
// Store the range of interval
// in arrL[] and arrR[].
for(int j = left; j <= right; j++)
{
if (arr[j] == maxValue)
{
arrL[j] = left;
arrR[j] = right;
}
}
}
}
}
// Function to find the largest interval
// for each query in Q[]
static void largestInterval(int arr[],
int query[],
int N, int Q)
{
// To store the L and R of X
int []arrL = new int[N + 1];
int []arrR = new int[N + 1];
// Function Call
utilLargestInterval(arr, arrL,
arrR, N);
// Iterate to find ranges for
// each query
for(int i = 0; i < Q; i++)
{
System.out.print("[" + arrL[query[i]] +
", " + arrR[query[i]] + "]\n");
}
}
// Driver Code
public static void main(String[] args)
{
int N = 5, Q = 3;
// Given array arr[]
int arr[] = { 0, 2, 1, 2, 3, 2 };
// Given queries
int query[] = { 1, 2, 4 };
// Function call
largestInterval(arr, query, N, Q);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
# Function to precompute the interval
# for each query
def utilLargestInterval(arr, arrL, arrR, N):
# For every values [1, N] find
# the longest intervals
for maxValue in range(1, N + 1):
lastIndex = 0
# Iterate the array arr[]
for i in range(N + 1):
if (lastIndex >= i or
arr[i] != maxValue):
continue
left = i
right = i
# Shift the left pointers
while (left > 0 and
arr[left] <= maxValue):
left -= 1
# Shift the right pointers
while (right <= N and
arr[right] <= maxValue):
right += 1
left += 1
right -= 1
lastIndex = right
# Store the range of interval
# in arrL[] and arrR[].
for j in range(left, right + 1):
if (arr[j] == maxValue):
arrL[j] = left
arrR[j] = right
# Function to find the largest interval
# for each query in Q[]
def largestInterval(arr, query, N, Q):
# To store the L and R of X
arrL = [0 for i in range(N + 1)]
arrR = [0 for i in range(N + 1)]
# Function call
utilLargestInterval(arr, arrL, arrR, N);
# Iterate to find ranges for each query
for i in range(Q):
print('[' + str(arrL[query[i]]) +
', ' + str(arrR[query[i]]) + ']')
# Driver code
if __name__=="__main__":
N = 5
Q = 3
# Given array arr[]
arr = [ 0, 2, 1, 2, 3, 2 ]
# Given Queries
query = [ 1, 2, 4 ]
# Function call
largestInterval(arr, query, N, Q)
# This code is contributed by rutvik_56
C#
// C# program for the above approach
using System;
class GFG{
// Function to precompute the interval
// for each query
static void utilLargestInterval(int []arr,
int []arrL,
int []arrR,
int N)
{
// For every values [1, N] find
// the longest intervals
for(int maxValue = 1;
maxValue <= N; maxValue++)
{
int lastIndex = 0;
// Iterate the array []arr
for(int i = 1; i <= N; i++)
{
if (lastIndex >= i ||
arr[i] != maxValue)
continue;
int left = i, right = i;
// Shift the left pointers
while (left > 0 &&
arr[left] <= maxValue)
left--;
// Shift the right pointers
while (right <= N &&
arr[right] <= maxValue)
right++;
left++;
right--;
lastIndex = right;
// Store the range of interval
// in arrL[] and arrR[].
for(int j = left; j <= right; j++)
{
if (arr[j] == maxValue)
{
arrL[j] = left;
arrR[j] = right;
}
}
}
}
}
// Function to find the largest interval
// for each query in Q[]
static void largestInterval(int []arr,
int []query,
int N, int Q)
{
// To store the L and R of X
int []arrL = new int[N + 1];
int []arrR = new int[N + 1];
// Function Call
utilLargestInterval(arr, arrL,
arrR, N);
// Iterate to find ranges for
// each query
for(int i = 0; i < Q; i++)
{
Console.Write("[" + arrL[query[i]] +
", " + arrR[query[i]] + "]\n");
}
}
// Driver Code
public static void Main(String[] args)
{
int N = 5, Q = 3;
// Given array []arr
int []arr = { 0, 2, 1, 2, 3, 2 };
// Given queries
int []query = { 1, 2, 4 };
// Function call
largestInterval(arr, query, N, Q);
}
}
// This code is contributed by Princi Singh
Javascript
[1, 3]
[2, 2]
[1, 5]
时间复杂度: O(Q + N 2 )
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live