最小化每个元素与 (i+K)%N 处元素的位置乘积之和
给定一个大小为N的整数数组Arr[] ,任务是找到给定函数在应用于数组时的最小值。
S(K) = 1 * Arr[(0 + K)%N] + 2 * Arr[(1 + K)%N] + 3 * Arr[(2 + K)%N] + . . . . . . . + N * Arr[((N-1) + K)%N]
= ∑[(i+1)∗Arr[(i+K)%N]
where K is a whole number.
例子:
Input: Arr[] = {2, 1, 4, 3}
Output: 20
Explanation: S(0) = 28, S(1) = 26, S(2) = 20, S(3) =26, S(4) = S(0), S(5) = S(1). . . . .
Thus there are only four possible values of the function and minimum is 20.
Input: Arr[] = {-3, 2, 1}
Output: -5
Explanation: S(0) = 4, S(1) = -5, S(2) = 1, S(3) = S(0), S(4) = S(1), S(5) = S(2) . . . . .
Thus there are only three possible values of the function and minimum is -5.
Naive Approach:解决该问题的基本方法是针对范围 [0, N-1] 内的所有 K 值计算函数的值。
请按照以下步骤解决问题:
- 可以观察到,如果Arr的长度为N ,那么给定函数只能有N个不同的值。
- 从k = 0 到 N – 1分别找出函数S(k)的所有可能值,并打印其中的最小值。
下面是朴素方法的实现。
C++
// C++ Program to find minimum value of given function
#include
using namespace std;
// Function to find value
// of given function at k
int fnValueAtk(vector Arr, int k)
{
int sum = 0;
int N = Arr.size();
for (int i = 0; i < N; i++) {
sum += (i + 1) * Arr[(i + k) % N];
}
return sum;
}
// Function to print minimum value
// of given function
void minValFn(vector Arr)
{
int N = Arr.size();
int min_val = INT_MAX;
for (int k = 0; k < N; k++) {
int fn_At_k = fnValueAtk(Arr, k);
min_val = min(min_val, fn_At_k);
}
cout << min_val;
}
int main()
{
vector Arr = { 2, 1, 4, 3 };
minValFn(Arr);
return 0;
}
// This code is contributed by hrithikgarg03188.
Java
// Java Program to find minimum value of given function
import java.util.*;
class GFG {
// Function to print minimum value
// of given function
static void minValFn(int[] Arr)
{
int N = Arr.length;
int min_val = Integer.MAX_VALUE;
for (int k = 0; k < N; k++) {
int fn_At_k = fnValueAtk(Arr, k);
min_val = Math.min(min_val, fn_At_k);
}
System.out.println(min_val);
}
// Function to find value
// of given function at k
static int fnValueAtk(int[] Arr, int k)
{
int sum = 0;
int N = Arr.length;
for (int i = 0; i < N; i++) {
sum += (i + 1) * Arr[(i + k) % N];
}
return sum;
}
// Driver code
public static void main(String[] args)
{
int[] Arr = { 2, 1, 4, 3 };
minValFn(Arr);
}
}
Python3
# Python3 program to find the minimum value of given function
import sys
# function to find the value
# of given function at k
def fnValueAtk(Arr, k):
sums = 0
N = len(Arr)
for i in range(N):
sums += (i + 1) * Arr[(i + k) % N]
return sums
# function to print minimum value of given function
def minValFn(Arr):
N = len(Arr)
min_val = sys.maxsize
for k in range(N):
fn_At_k = fnValueAtk(Arr, k)
min_val = min(min_val, fn_At_k)
print(min_val)
# Driver Code
Arr = [2, 1, 4, 3]
minValFn(Arr)
# This code is contributed by phasing17
C#
// C# Program to find minimum value of given function
using System;
public class GFG{
// Function to print minimum value
// of given function
static void minValFn(int[] Arr)
{
int N = Arr.Length;
int min_val = Int32.MaxValue;
for (int k = 0; k < N; k++) {
int fn_At_k = fnValueAtk(Arr, k);
min_val = Math.Min(min_val, fn_At_k);
}
Console.WriteLine(min_val);
}
// Function to find value
// of given function at k
static int fnValueAtk(int[] Arr, int k)
{
int sum = 0;
int N = Arr.Length;
for (int i = 0; i < N; i++) {
sum += (i + 1) * Arr[(i + k) % N];
}
return sum;
}
// Driver code
static public void Main (){
int[] Arr = { 2, 1, 4, 3 };
minValFn(Arr);
}
}
// This code is contributed by hrithikgarg03188.
Javascript
// JavaScript program to find the
// minimum value of the given function
// function to find the value of
// given function at k
function fnValueAtk(Arr, k)
{
var sum = 0;
var N = Arr.length;
for (var i = 0; i < N; i++)
{
sum += (i + 1) * Arr[(i + k) % N];
}
return sum;
}
// function to print the minimum value
// of given function
function minValFn(Arr)
{
var N = Arr.length;
var min_val = Number.MAX_SAFE_INTEGER;
for (var k = 0; k < N; k++)
{
var fn_At_k = fnValueAtk(Arr, k);
min_val = Math.min(min_val, fn_At_k);
}
document.write(min_val);
}
// Driver Code
var Arr = [2, 1, 4, 3];
minValFn(Arr);
// This code is contributed by phasing17
C++
// C++ Program to find minimum value
// Of given function
#include
using namespace std;
void minValFn(vector Arr)
{
int N = Arr.size();
int min_val;
// To store sum of all elements
int sum = 0;
// Finding S(0)
int s0 = 0;
for (int i = 0; i < N; i++) {
s0 += Arr[i] * (i + 1);
sum += Arr[i];
}
min_val = s0;
// To store s(k-1)
int sk_min_1 = s0;
for (int k = 1; k < N; k++) {
// Using formula
int sk = sk_min_1 - sum + Arr[(k - 1) % N] * N;
min_val = min(sk, min_val);
sk_min_1 = sk;
}
cout << min_val << "\n";
}
// Driver code
int main()
{
vector Arr = { 2, 1, 4, 3 };
minValFn(Arr);
}
// This code is contributed by Taranpreet
Java
// Java Program to find minimum value
// Of given function
import java.io.*;
public class GFG {
static void minValFn(int[] Arr)
{
int N = Arr.length;
int min_val;
// To store sum of all elements
int sum = 0;
// Finding S(0)
int s0 = 0;
for (int i = 0; i < N; i++) {
s0 += Arr[i] * (i + 1);
sum += Arr[i];
}
min_val = s0;
// To store s(k-1)
int sk_min_1 = s0;
for (int k = 1; k < N; k++) {
// Using formula
int sk = sk_min_1 - sum
+ Arr[(k - 1) % N] * N;
min_val = Math.min(sk, min_val);
sk_min_1 = sk;
}
System.out.println(min_val);
}
// Driver code
public static void main(String[] args)
{
int[] Arr = { 2, 1, 4, 3 };
minValFn(Arr);
}
}
Python3
# Python3 program to find the minimum value of
# the given function
# function to find the minimum value
def minValFn(Arr):
N = len(Arr)
# to store sum of all elements
sums = 0
# finding S(0)
s0 = 0
for i in range(N):
s0 += Arr[i] * (i + 1)
sums += Arr[i]
min_val = s0
# To store s(k - 1)
sk_min_1 = s0
for k in range(1, N):
# using formula for minimum value
sk = sk_min_1 - sums + Arr[(k - 1) % N] * N
min_val = min(sk, min_val)
sk_min_1 = sk
print(min_val)
# Driver code
Arr = [2, 1, 4, 3]
minValFn(Arr)
# This code is contributed by phasing17
C#
// C# Program to find minimum value
// Of given function
using System;
public class GFG {
static void minValFn(int[] Arr)
{
int N = Arr.Length;
int min_val;
// To store sum of all elements
int sum = 0;
// Finding S(0)
int s0 = 0;
for (int i = 0; i < N; i++) {
s0 += Arr[i] * (i + 1);
sum += Arr[i];
}
min_val = s0;
// To store s(k-1)
int sk_min_1 = s0;
for (int k = 1; k < N; k++) {
// Using formula
int sk = sk_min_1 - sum
+ Arr[(k - 1) % N] * N;
min_val = Math.Min(sk, min_val);
sk_min_1 = sk;
}
Console.Write(min_val);
}
// Driver code
public static void Main(string[] args)
{
int[] Arr = { 2, 1, 4, 3 };
minValFn(Arr);
}
}
// This code is contributed by ukasp.
JavaScript
20
时间复杂度: O(N 2 )。
辅助空间: O(1)
高效方法:解决问题的有效方法基于S(k)和S(k-1) 之间的以下关系:
S(k) = S(k – 1) – Sum + Arr[(k-1)%N] * N
请参阅下图以获得更好的理解。
插图:
Suppose the array is {2, 1, 4, 3}
Now, to find the value of the function given in the problem:
= 1 * Arr[(0 + k)%N] + 2 * Arr[(1 + k)%N] + 3 * Arr[(2 + k)%N] + . . . . . . . + N * Arr[((N-1) + k)%N]
- Now, for k = 0;
S(0) = 1* Arr[0] + 2* Arr[1] + 3* Arr[2] + 4* Arr[3]
= 1*2 + 2*1 + 3*4 + 4*3
= 28 - similarly, for k = 1;
S(1) = 1* Arr[1] + 2* Arr[2] + 3* Arr[3] + 4* Arr[0]
= 1*1 + 2*4 + 3*3 + 4*2
= 26 - similarly, for k = 2;
S(2) = 20 - similarly, for k = 3;
s(3) = 26 - Now, S(4) = S(0), S(5) = S(1). . . . and so on.
Hence the minimum value of function is S(2) = 20;
Now, it can be solved using derived equation: S(k) = S(k – 1) – Sum + Arr[(k-1)%N] * N
Firstly, store the value of S(0) (k = 0) in some variable by traversing the array and then use the above mentioned equation to find the values of function for k = 1, 2, 3 and so on.
Later compare the value of S(k) with S(0) and update it with the minimum one. Follow the below steps:
- Now, the value of function when k = 0 S(0) is 28, and total sum is 10, then to find the value for k = 1
using equation,
for k = 1: S(1) = 28 – 10 + Arr[(1 – 1) % 4] * 4
= 28 – 10 + 8
= 26 (which is less than S(0) hence update the minimum value with S(1)) - For k = 2: S(2) = 26 – 10 + Arr[(2 – 1) % 4]* 4
= 26 – 10 + 4
= 20 (which is less than S(1) hence update the minimum value with S(2)) - For k = 3: S(3) = 20 – 10 + Arr[(3 – 1) % 4]* 4
= 20 – 10 + 16
= 26 (which is not less than S(2) hence, do not update the value with S(3))
Hence, S(2) is the minimum value of function.
按照下面提到的步骤来实现上述观察:
- 声明一个变量'min_val'来存储给定函数的最小值。
- 在数组的单次遍历中找到S(0)并将其存储在'min_val'中。
- 然后使用上面的等式找到k = 1 到 N – 1的函数S(k)的所有可能值。
- 如果此值小于“min_val”,则更新“ min_val” 。
- min_val的最终值是所需的答案。
下面是上述方法的实现:
C++
// C++ Program to find minimum value
// Of given function
#include
using namespace std;
void minValFn(vector Arr)
{
int N = Arr.size();
int min_val;
// To store sum of all elements
int sum = 0;
// Finding S(0)
int s0 = 0;
for (int i = 0; i < N; i++) {
s0 += Arr[i] * (i + 1);
sum += Arr[i];
}
min_val = s0;
// To store s(k-1)
int sk_min_1 = s0;
for (int k = 1; k < N; k++) {
// Using formula
int sk = sk_min_1 - sum + Arr[(k - 1) % N] * N;
min_val = min(sk, min_val);
sk_min_1 = sk;
}
cout << min_val << "\n";
}
// Driver code
int main()
{
vector Arr = { 2, 1, 4, 3 };
minValFn(Arr);
}
// This code is contributed by Taranpreet
Java
// Java Program to find minimum value
// Of given function
import java.io.*;
public class GFG {
static void minValFn(int[] Arr)
{
int N = Arr.length;
int min_val;
// To store sum of all elements
int sum = 0;
// Finding S(0)
int s0 = 0;
for (int i = 0; i < N; i++) {
s0 += Arr[i] * (i + 1);
sum += Arr[i];
}
min_val = s0;
// To store s(k-1)
int sk_min_1 = s0;
for (int k = 1; k < N; k++) {
// Using formula
int sk = sk_min_1 - sum
+ Arr[(k - 1) % N] * N;
min_val = Math.min(sk, min_val);
sk_min_1 = sk;
}
System.out.println(min_val);
}
// Driver code
public static void main(String[] args)
{
int[] Arr = { 2, 1, 4, 3 };
minValFn(Arr);
}
}
Python3
# Python3 program to find the minimum value of
# the given function
# function to find the minimum value
def minValFn(Arr):
N = len(Arr)
# to store sum of all elements
sums = 0
# finding S(0)
s0 = 0
for i in range(N):
s0 += Arr[i] * (i + 1)
sums += Arr[i]
min_val = s0
# To store s(k - 1)
sk_min_1 = s0
for k in range(1, N):
# using formula for minimum value
sk = sk_min_1 - sums + Arr[(k - 1) % N] * N
min_val = min(sk, min_val)
sk_min_1 = sk
print(min_val)
# Driver code
Arr = [2, 1, 4, 3]
minValFn(Arr)
# This code is contributed by phasing17
C#
// C# Program to find minimum value
// Of given function
using System;
public class GFG {
static void minValFn(int[] Arr)
{
int N = Arr.Length;
int min_val;
// To store sum of all elements
int sum = 0;
// Finding S(0)
int s0 = 0;
for (int i = 0; i < N; i++) {
s0 += Arr[i] * (i + 1);
sum += Arr[i];
}
min_val = s0;
// To store s(k-1)
int sk_min_1 = s0;
for (int k = 1; k < N; k++) {
// Using formula
int sk = sk_min_1 - sum
+ Arr[(k - 1) % N] * N;
min_val = Math.Min(sk, min_val);
sk_min_1 = sk;
}
Console.Write(min_val);
}
// Driver code
public static void Main(string[] args)
{
int[] Arr = { 2, 1, 4, 3 };
minValFn(Arr);
}
}
// This code is contributed by ukasp.
JavaScript
20
时间复杂度: O(N)
辅助空间: O(1)