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

📅  最后修改于: 2021-05-17 03:55:02             🧑  作者: 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. i = 2遍历数组arr []N-1并计算min(arr [i],dp1 [i-1] + 1)
  3. i = N-11遍历数组arr []并计算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)