最小化对的总和,在删除时将数组分成 3 个子数组
给定一个大小为N的数组arr ,任务是找到具有最小和的元素对,在删除时将数组分解为原始数组的 3 个非空子数组。打印这对中元素的总和。
Input: arr[]: {4, 2, 1, 2, 4}
Output: 4
Explanation:
Minimum sum pairs are values (2, 1) and (1, 2) but selecting a pair with adjacent indices won’t break the array into 3 parts. Next minimum value pair is (2, 2), which divides the array into {4}, {1}, {4}. Hence the answer is 2 + 2 = 4.
Input: arr[] = {5, 2, 4, 6, 3, 7}
Output: 5
Explanation:
Among all the pairs which break the array into 3 subparts, pair with values (2, 3) gives minimum sum.
天真的方法:将数组分成三个子数组。需要删除的两个元素都应遵循以下条件:
- 无法选择任何端点(索引0和N-1 )。
- 不能选择相邻的索引。
因此,找到所有可能的配对组合,并选择符合上述条件且总和最小的配对。
时间复杂度: O(N 2 )
辅助空间: O(N)
有效方法:按照以下步骤有效解决此问题:
- 创建一个数组 prefixMin,其中第 i 个元素表示数组 arr 中从第 0 到第 i 个索引的最小元素。
- 创建一个变量 ans,用于存储最终答案并使用 INT_MAX 对其进行初始化。
- 现在,运行 i=3 到 i=N-1 的循环(从 3 开始,因为不能选择第 0 个,这个循环是选择对的第二个元素,这意味着它甚至不能从 2 开始因为那么第一个元素的唯一剩余选项是 1 和 2,它们都不遵循给定条件)并且在每次迭代中将 ans 更改为 ans 和 arr[i]+prefixMin[i-2] 中的最小值.
- 执行上述步骤后,打印 ans。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to find minimum possible sum
// of pair which breaks the array into 3
// non-empty subarrays
int minSumPair(int arr[], int N)
{
if (N < 5) {
return -1;
}
int prefixMin[N];
prefixMin[0] = arr[0];
// prefixMin[i] contains minimum element till i
for (int i = 1; i < N - 1; i++) {
prefixMin[i] = min(arr[i], prefixMin[i - 1]);
}
int ans = INT_MAX;
for (int i = 3; i < N - 1; i++) {
ans = min(ans, arr[i] + prefixMin[i - 2]);
}
return ans;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 5, 2, 4, 6, 3, 7 };
int N = sizeof(arr) / sizeof(int);
cout << minSumPair(arr, N) << endl;
return 0;
}
Java
// Java implementation of the above approach
class GFG{
// Function to find minimum possible sum
// of pair which breaks the array into 3
// non-empty subarrays
static int minSumPair(int arr[], int N)
{
if (N < 5) {
return -1;
}
int []prefixMin = new int[N];
prefixMin[0] = arr[0];
// prefixMin[i] contains minimum element till i
for (int i = 1; i < N - 1; i++) {
prefixMin[i] = Math.min(arr[i], prefixMin[i - 1]);
}
int ans = Integer.MAX_VALUE;
for (int i = 3; i < N - 1; i++) {
ans = Math.min(ans, arr[i] + prefixMin[i - 2]);
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Given array
int arr[] = { 5, 2, 4, 6, 3, 7 };
int N = arr.length;
System.out.print(minSumPair(arr, N) +"\n");
}
}
// This code is contributed by Princi Singh
Python3
# Python 3 implementation of the above approach
import sys
# Function to find minimum possible sum
# of pair which breaks the array into 3
# non-empty subarrays
def minSumPair(arr, N):
if(N < 5):
return -1
prefixMin = [0 for i in range(N)]
prefixMin[0] = arr[0]
# prefixMin[i] contains minimum element till i
for i in range(1,N - 1,1):
prefixMin[i] = min(arr[i], prefixMin[i - 1])
ans = sys.maxsize
for i in range(3,N - 1,1):
ans = min(ans, arr[i] + prefixMin[i - 2])
return ans
# Driver Code
if __name__ == '__main__':
# Given array
arr = [5, 2, 4, 6, 3, 7]
N = len(arr)
print(minSumPair(arr, N))
# This code is contributed by ipg201107.
C#
// C# implementation of the above approach
using System;
public class GFG{
// Function to find minimum possible sum
// of pair which breaks the array into 3
// non-empty subarrays
static int minSumPair(int []arr, int N)
{
if (N < 5) {
return -1;
}
int []prefixMin = new int[N];
prefixMin[0] = arr[0];
// prefixMin[i] contains minimum element till i
for (int i = 1; i < N - 1; i++) {
prefixMin[i] = Math.Min(arr[i], prefixMin[i - 1]);
}
int ans = int.MaxValue;
for (int i = 3; i < N - 1; i++) {
ans = Math.Min(ans, arr[i] + prefixMin[i - 2]);
}
return ans;
}
// Driver Code
public static void Main(String[] args)
{
// Given array
int []arr = { 5, 2, 4, 6, 3, 7 };
int N = arr.Length;
Console.Write(minSumPair(arr, N) +"\n");
}
}
// This code is contributed by shikhasingrajput
Javascript
5
时间复杂度: O(n)
空间复杂度: O(n)