📌  相关文章
📜  通过用最小可能值 arr[j] + |j – i| 替换每个数组元素来修改数组

📅  最后修改于: 2021-09-05 11:54:11             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是为每个索引找到一个值,使得索引i处的值是arr[j] + |j – i|其中1 ≤ j ≤ N ,任务是为从1 到 N 的每个索引找到最小值。

例子:

朴素的方法:这个想法是使用两个嵌套的 for 循环来遍历数组,并为每个i索引找到并打印arr[j] + |ij|的最小值 .

时间复杂度: O(N 2 )
辅助空间: O(N)

高效的方法:这个想法是从左右数组遍历中使用前缀和技术,并找到每个索引的最小值。请按照以下步骤解决问题:

  1. 取两个辅助数组dp1[]dp2[] ,其中dp1[]存储从左到右遍历的答案, dp2[]存储从右到左遍历的答案。
  2. 将数组arr[]i = 2遍历到N-1并计算min(arr[i], dp1[i-1] + 1)
  3. 遍历数组arr[]i = N-11并计算min(arr[i], dp2[i+1] + 1)
  4. 再次从1N遍历数组,并在每次迭代时打印min(dp1[i], dp2[i])

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find minimum value of
// arr[j] + |j - i| for every array index
void minAtEachIndex(int n, int arr[])
{
    // Stores minimum of a[j] + |i - j|
    // upto position i
    int dp1[n];
 
    // Stores minimum of a[j] + |i-j|
    // upto position i from the end
    int dp2[n];
 
    int i;
 
    dp1[0] = arr[0];
 
    // Traversing and storing minimum
    // of a[j]+|i-j| upto i
    for (i = 1; i < n; i++)
        dp1[i] = min(arr[i], dp1[i - 1] + 1);
 
    dp2[n - 1] = arr[n - 1];
 
    // Traversing and storing minimum
    // of a[j]+|i-j| upto i from the end
    for (i = n - 2; i >= 0; i--)
        dp2[i] = min(arr[i], dp2[i + 1] + 1);
 
    vector v;
 
    // Traversing from [0, N] and storing minimum
    // of a[j] + |i - j| from starting and end
    for (i = 0; i < n; i++)
        v.push_back(min(dp1[i], dp2[i]));
 
    // Print the required array
    for (auto x : v)
        cout << x << " ";
}
 
// Driver code
int main()
{
 
    // Given array arr[]
    int arr[] = { 1, 4, 2, 5, 3 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    minAtEachIndex(N, arr);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
import java.util.ArrayList;
import java.util.List;
   
class GFG{
       
// Function to find minimum value of
// arr[j] + |j - i| for every array index
static void minAtEachIndex(int n, int arr[])
{
     
    // Stores minimum of a[j] + |i - j|
    // upto position i
    int dp1[] = new int[n];
     
    // Stores minimum of a[j] + |i-j|
    // upto position i from the end
    int dp2[] = new int[n];
 
    int i;
 
    dp1[0] = arr[0];
 
    // Traversing and storing minimum
    // of a[j]+|i-j| upto i
    for(i = 1; i < n; i++)
        dp1[i] = Math.min(arr[i], dp1[i - 1] + 1);
 
    dp2[n - 1] = arr[n - 1];
 
    // Traversing and storing minimum
    // of a[j]+|i-j| upto i from the end
    for(i = n - 2; i >= 0; i--)
        dp2[i] = Math.min(arr[i], dp2[i + 1] + 1);
 
    ArrayList v = new ArrayList();
 
    // Traversing from [0, N] and storing minimum
    // of a[j] + |i - j| from starting and end
    for(i = 0; i < n; i++)
        v.add(Math.min(dp1[i], dp2[i]));
 
    // Print the required array
    for(int x : v)
        System.out.print(x + " ");
}
   
// Driver code
public static void main(String[] args)
{
     
    // Given array arr[]
    int arr[] = { 1, 4, 2, 5, 3 };
 
    // Size of the array
    int N = arr.length;
 
    // Function Call
    minAtEachIndex(N, arr);
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to find minimum value of
# arr[j] + |j - i| for every array index
def minAtEachIndex(n, arr):
     
    # Stores minimum of a[j] + |i - j|
    # upto position i
    dp1 = [0] * n
     
    # Stores minimum of a[j] + |i-j|
    # upto position i from the end
    dp2 = [0] * n
     
    i = 0
 
    dp1[0] = arr[0]
     
    # Traversing and storing minimum
    # of a[j]+|i-j| upto i
    for i in range(1, n):
        dp1[i] = min(arr[i], dp1[i - 1] + 1)
 
    dp2[n - 1] = arr[n - 1]
 
    # Traversing and storing minimum
    # of a[j]+|i-j| upto i from the end
    for i in range(n - 2, -1, -1):
        dp2[i] = min(arr[i], dp2[i + 1] + 1)
 
    v = []
 
    # Traversing from [0, N] and storing minimum
    # of a[j] + |i - j| from starting and end
    for i in range(0, n):
        v.append(min(dp1[i], dp2[i]))
 
    # Print the required array
    for x in v:
        print(x, end = " ")
 
# Driver code
if __name__ == '__main__':
     
    # Given array arr
    arr = [ 1, 4, 2, 5, 3 ]
 
    # Size of the array
    N = len(arr)
 
    # Function Call
    minAtEachIndex(N, arr)
 
# This code is contributed by shikhasingrajput


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
       
// Function to find minimum value of
// arr[j] + |j - i| for every array index
static void minAtEachIndex(int n, int []arr)
{
     
    // Stores minimum of a[j] + |i - j|
    // upto position i
    int []dp1 = new int[n];
     
    // Stores minimum of a[j] + |i-j|
    // upto position i from the end
    int []dp2 = new int[n];
    int i;
    dp1[0] = arr[0];
 
    // Traversing and storing minimum
    // of a[j]+|i-j| upto i
    for(i = 1; i < n; i++)
        dp1[i] = Math.Min(arr[i], dp1[i - 1] + 1);
    dp2[n - 1] = arr[n - 1];
 
    // Traversing and storing minimum
    // of a[j]+|i-j| upto i from the end
    for(i = n - 2; i >= 0; i--)
        dp2[i] = Math.Min(arr[i], dp2[i + 1] + 1);
    List v = new List();
 
    // Traversing from [0, N] and storing minimum
    // of a[j] + |i - j| from starting and end
    for(i = 0; i < n; i++)
        v.Add(Math.Min(dp1[i], dp2[i]));
 
    // Print the required array
    foreach(int x in v)
        Console.Write(x + " ");
}
   
// Driver code
public static void Main(String[] args)
{
     
    // Given array []arr
    int []arr = { 1, 4, 2, 5, 3 };
 
    // Size of the array
    int N = arr.Length;
 
    // Function Call
    minAtEachIndex(N, arr);
}
}
 
// This code is contributed by shikhasingrajput


输出:
1 2 2 3 3

时间复杂度: O(N)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live