给定一个由N 个整数组成的数组A[] (基于 1 的索引),任务是找到函数的最小值对于X 的任何可能值。
例子:
Input: A[] = {1, 2, 3, 4}
Output: 0
Explanation:
Consider the value of X as 0, then the value of the given function is (1 – 1 + 2 – 2 + 3 – 3 + 4 – 4) = 0, which is minimum.
Input: A[] = {5, 3, 9}
Output: 5
方法:根据以下观察可以解决给定的问题:
- 考虑一个函数为(B[i] = A[i] − i) ,然后最小化 ,想法是选择X的值作为数组B[]的中值,使得总和最小。
请按照以下步骤解决问题:
- 初始化一个数组,比如说B[] ,它为数组A[] 的每个可能值存储(A[i] – i)的值。
- 遍历给定的数组A[]并且对于每个索引i ,将B[i]的值更新为(A[i] – i) 。
- 按升序对数组B[]进行排序,并找到值X作为数组B[] 的中值。
- 完成以上步骤后,求给定函数的值对于X的计算值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum value of
// the given function
int minimizeFunction(int A[], int N)
{
// Stores the value of A[i] - i
int B[N];
// Traverse the given array A[]
for (int i = 0; i < N; i++) {
// Update the value of B[i]
B[i] = A[i] - i - 1;
}
// Sort the array B[]
sort(B, B + N);
// Calculate the median of the
// array B[]
int median = (B[int(floor((N - 1) / 2.0))]
+ B[int(ceil((N - 1) / 2.0))])
/ 2;
// Stores the minimum value of
// the function
int minValue = 0;
for (int i = 0; i < N; i++) {
// Update the minValue
minValue += abs(A[i] - (median + i + 1));
}
// Return the minimum value
return minValue;
}
// Driver Code
int main()
{
int A[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int N = sizeof(A) / sizeof(A[0]);
cout << minimizeFunction(A, N);
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
import java.lang.Math;
import java.util.*;
class GFG {
public static int minimizeFunction(int A[], int N)
{
// Stores the value of A[i] - i
int B[] = new int[N];
// Traverse the given array A[]
for (int i = 0; i < N; i++) {
// Update the value of B[i]
B[i] = A[i] - i - 1;
}
// Sort the array B[]
Arrays.sort(B);
// Calculate the median of the
// array B[]
int median = (B[(int)(Math.floor((N - 1) / 2.0))]
+ B[(int)(Math.ceil((N - 1) / 2.0))])
/ 2;
// Stores the minimum value of
// the function
int minValue = 0;
for (int i = 0; i < N; i++) {
// Update the minValue
minValue += Math.abs(A[i] - (median + i + 1));
}
// Return the minimum value
return minValue;
}
public static void main(String[] args)
{
int A[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int N = A.length;
System.out.println(minimizeFunction(A, N));
}
}
// This code is contributed by sam_2200.
Python3
# Python3 program for the above approach
from math import floor, ceil
# Function to find minimum value of
# the given function
def minimizeFunction(A, N):
# Stores the value of A[i] - i
B = [0] * N
# Traverse the given array A[]
for i in range(N):
# Update the value of B[i]
B[i] = A[i] - i - 1
# Sort the array B[]
B = sorted(B)
# Calculate the median of the
# array B[]
x, y = int(floor((N - 1) / 2.0)), int(ceil((N - 1) / 2.0))
median = (B[x] + B[y]) / 2
# Stores the minimum value of
# the function
minValue = 0
for i in range(N):
# Update the minValue
minValue += abs(A[i] - (median + i + 1))
# Return the minimum value
return int(minValue)
# Driver Code
if __name__ == '__main__':
A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
N = len(A)
print(minimizeFunction(A, N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG {
public static int minimizeFunction(int[] A, int N)
{
// Stores the value of A[i] - i
int[] B = new int[N];
// Traverse the given array A[]
for (int i = 0; i < N; i++) {
// Update the value of B[i]
B[i] = A[i] - i - 1;
}
// Sort the array B[]
Array.Sort(B);
// Calculate the median of the
// array B[]
int median = (B[(int)(Math.Floor((N - 1) / 2.0))] + B[(int)(Math.Ceiling((N - 1) / 2.0))])
/ 2;
// Stores the minimum value of
// the function
int minValue = 0;
for (int i = 0; i < N; i++) {
// Update the minValue
minValue += Math.Abs(A[i] - (median + i + 1));
}
// Return the minimum value
return minValue;
}
static void Main()
{
int []A = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int N = A.Length;
Console.WriteLine(minimizeFunction(A, N));
}
}
// This code is contributed by SoumikMondal.
Javascript
输出:
0
时间复杂度: O(N * log N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。