对于每个 Array 索引,找到所有 M 操作中的最大值
给定一个大小为N的数组arr[]最初用 0 填充,另一个数组Positions[]的大小为B ,任务是在执行以下M操作后返回每个索引的最大值:
- 使索引Positions[i]处的值等于0
- Positions[i]右边的所有数字都将比它们的左邻大一。
- Positions[i]左边的所有数字都将比它们的右邻大一。
例子:
Input: N = 6, M = 2, Positions = {2, 3}
Output: {3, 2, 1, 1, 2, 3}
Explanation: Initial array: {0, 0, 0, 0, 0, 0}
After first operation: {2, 1, 0, 1, 2, 3}
where each element to the right of 2nd index and each element to its left follow the given condition
After second operation: {3, 2, 1, 0, 1, 2}
Thus, maximum value of each index among all the operations: {3, 2, 1, 1, 2, 3}
Input: N = 4, M = 3, Positions = {3, 2, 1}
Output: {3, 2, 1, 2}
Explanation: Initial array: {0, 0, 0, 0}
After first operation: {3, 2, 1, 0}
After second operation: {2, 1, 0, 1}
After third operation: {1, 0, 1, 2}
Thus, Maximum: {3, 2, 1, 2}
方法:可以根据以下观察解决问题:
The value set at the indices for each operation denotes the distance from the index set to 0.
Therefore, the positions closest to the ends of the array which are set to 0 will set the values of other indices to be the maximum when given operation is performed.
基于上述观察,解决方案是找到最接近数组末端的索引(比如x和y ),这些索引在任何操作中都设置为 0。每个索引的最大值将是与x和y中任何一个的最大距离。
请按照以下步骤解决问题:
- 生成一个具有空值的数组(比如arr[] )。
- 在Positions[]数组(比如x和y )中找到最大值和最小值。
- 从i = 0 到 N-1遍历数组:
- 打印当前索引与x或y之间绝对差的最大值
下面是上述方法的实现:
C++
// C++ code for the above approach:
#include
using namespace std;
// Function to find the maximum values
// for each array index among M operations
vector create(vector& x, int n, int m)
{
int maxa = 0;
int mini = INT_MAX;
// Traversing the position array
for (int j = 0; j < m; j++) {
maxa = max(maxa, x[j]);
mini = min(mini, x[j]);
}
vector arr;
// Traversing the array
for (int k = 0; k < n; k++) {
// Print maximum of absolute
// difference between maximum
// and minimum positions to
// current position
arr.push_back(max(abs(k - maxa), abs(k - mini)));
}
return arr;
}
// Driver code
int main()
{
int N = 4;
int M = 3;
// Initializing a position array
vector Positions = { 3, 2, 1 };
vector sol = create(Positions, N, M);
for (auto x : sol)
cout << x << " ";
return 0;
}
// This code is contributed by rakeshsahni
Java
// Java code for the above approach:
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to find the maximum values
// for each array index among M operations
public static List create(int x[],
int n,
int m)
{
int maxa = 0;
int mini = Integer.MAX_VALUE;
// Traversing the position array
for (int j = 0; j < m; j++) {
maxa = Math.max(maxa, x[j]);
mini = Math.min(mini, x[j]);
}
List arr
= new ArrayList();
// Traversing the array
for (int k = 0; k < n; k++) {
// Print maximum of absolute
// difference between maximum
// and minimum positions to
// current position
arr.add(Math.max(
Math.abs(k - maxa),
Math.abs(k - mini)));
}
return arr;
}
// Driver code
public static void main(String[] args)
{
int N = 4;
int M = 3;
// Initializing a position array
int Positions[] = { 3, 2, 1 };
List sol
= create(Positions, N, M);
for (int x : sol)
System.out.print(x + " ");
}
}
3 2 1 2
时间复杂度: O(N) + O(M),其中 N 是数组的大小,M 是位置数组的大小
辅助空间: O(M)