通过从第二个数组中选择索引大于第一个数组的元素来查找最小对和
给定两个大小为N的数组A[]和B[] ,任务是最小化A[i] + B[j]使得j ≥ i 。
例子:
Input: A[] = {34, 12, 45, 10, 86, 39, 77},
B[] = {5, 42, 29, 63, 30, 33, 20}
Output: 30
Explanation: For minimizing the sum, take i = 3 and j = 6.
Input: A[] = {34, 17, 45, 10, 19},
B[] = {2, 13, 7, 11, 16}
Output: 21
Explanation: For minimizing the sum, take both i and j = 3.
朴素方法:解决这个问题的朴素方法是使用2 个 for 循环,一个用于迭代A[] ,另一个用于迭代B[] 。只需检查并比较所有可能性以最小化总和。
下面是上述幼稚方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to minimize the sum
int minimumCost(int A[], int B[], int N)
{
int minimuPrice = INT_MAX;
// Checking and comparing for
// all the possibilities
for (int i = 0; i < N; i++) {
for (int j = i; j < N; j++) {
int currentPrice = A[i] + B[j];
minimuPrice = min(minimuPrice,
currentPrice);
}
}
// Return the minimum price found
return minimuPrice;
}
// Driver Code
int main()
{
int A[] = { 34, 12, 45, 10, 86, 39, 77 };
int B[] = { 5, 42, 29, 63, 30, 33, 20 };
int N = sizeof(A) / sizeof(A[0]);
// Function Call
cout << minimumCost(A, B, N);
return 0;
}
Java
// Java program for above approach
import java.io.*;
class GFG {
// Function to minimize the sum
public static int minimumCost(int[] A,
int[] B,
int N)
{
int minimuPrice = Integer.MAX_VALUE;
// Checking and comparing
// for all the possibilities
for (int i = 0; i < N; i++) {
for (int j = i; j < N; j++) {
int currentPrice
= A[i] + B[j];
minimuPrice
= Math.min(minimuPrice,
currentPrice);
}
}
return minimuPrice;
}
// Driver Code
public static void main(String[] args)
{
int[] A = { 34, 12, 45, 10, 86, 39, 77 };
int[] B = { 5, 42, 29, 63, 30, 33, 20 };
int N = A.length;
System.out.println(minimumCost(A, B, N));
}
}
Python
# Python program for above approach
# import the module
import sys
# Function to minimize the sum
def minimumCost(A, B, N):
minimuPrice = sys.maxint
# Checking and comparing for
# all the possibilities
for i in range(N):
for j in range(i, N):
currentPrice = A[i] + B[j]
minimuPrice = min(minimuPrice,
currentPrice)
# Return the minimum price found
return minimuPrice;
# Driver Code
if __name__ == "__main__":
A = [ 34, 12, 45, 10, 86, 39, 77 ]
B = [ 5, 42, 29, 63, 30, 33, 20 ]
N = len(A)
# Function Call
print(minimumCost(A, B, N))
# This code is contributed by hrithikgarg03188.
C#
// C# program for above approach
using System;
class GFG
{
// Function to minimize the sum
static int minimumCost(int[] A, int[] B, int N)
{
int minimuPrice = Int32.MaxValue;
// Checking and comparing for
// all the possibilities
for (int i = 0; i < N; i++) {
for (int j = i; j < N; j++) {
int currentPrice = A[i] + B[j];
minimuPrice
= Math.Min(minimuPrice, currentPrice);
}
}
// Return the minimum price found
return minimuPrice;
}
// Driver Code
public static int Main()
{
int[] A = { 34, 12, 45, 10, 86, 39, 77 };
int[] B = { 5, 42, 29, 63, 30, 33, 20 };
int N = A.Length;
// Function Call
Console.Write(minimumCost(A, B, N));
return 0;
}
}
// This code is contributed by Taranpreet
Javascript
C++
// C++ program for above approach
#include
using namespace std;
// Function to calculate minimum sum
int minimumCost(int A[], int B[], int N)
{
// For storing the minimum value
// from rear end of B[]
int cheapestPossible[N];
// Only one possible value on last index
cheapestPossible[N - 1] = B[N - 1];
for (int i = (N - 2); i >= 0; i--) {
// The lowest possible sum at
// index i and above
cheapestPossible[i]
= min(cheapestPossible[i + 1],
B[i]);
}
// For storing minimum sum
int minimumPrice = INT_MAX;
// Adding the current value of A[] and
// minimum value of B[] after i and
// comparing it with the last minimum sum
for (int i = 0; i < N; i++) {
minimumPrice
= min(minimumPrice,
A[i] + cheapestPossible[i]);
}
return minimumPrice;
}
// Driver Code
int main()
{
int A[] = { 34, 12, 45, 10, 86, 39, 77 };
int B[] = { 5, 42, 29, 63, 30, 33, 20 };
int N = sizeof(A) / sizeof(A[0]);
// Function Call
cout << minimumCost(A, B, N);
return 0;
}
Java
// Java program for above approach
import java.io.*;
class GFG {
// Function to calculate minimum sum
public static int minimumCost(int[] A,
int[] B,
int N)
{
// For storing the minimum value
// from rear end of B[]
int[] cheapestPossible = new int[N];
// Only one possible value
// on last index
cheapestPossible[N - 1]
= B[N - 1];
for (int i = (N - 2); i >= 0; i--) {
// The lowest possible sum at
// index i and above
cheapestPossible[i]
= Math.min(cheapestPossible[i + 1],
B[i]);
}
// For storing minimum sum
int minimumPrice = Integer.MAX_VALUE;
// Adding the current value of A[]
// and minimum value of B[] after i
// and comparing it with
// the last minimum sum obtained
for (int i = 0; i < N; i++) {
minimumPrice = Math.min(
minimumPrice, A[i]
+ cheapestPossible[i]);
}
return minimumPrice;
}
// Driver code
public static void main(String[] args)
{
int[] A = { 34, 12, 45, 10, 86, 39, 77 };
int[] B = { 5, 42, 29, 63, 30, 33, 20 };
int N = A.length;
System.out.println(minimumCost(A, B, N));
}
}
Python3
# Python 3 program for above approach
import sys
# Function to calculate minimum sum
def minimumCost(A, B, N):
# For storing the minimum value
# from rear end of B[]
cheapestPossible = [0]*N
# Only one possible value on last index
cheapestPossible[N - 1] = B[N - 1]
for i in range(N - 2 ,- 1, -1):
# The lowest possible sum at
# index i and above
cheapestPossible[i] = min(cheapestPossible[i + 1],
B[i])
# For storing minimum sum
minimumPrice = sys.maxsize
# Adding the current value of A[] and
# minimum value of B[] after i and
# comparing it with the last minimum sum
for i in range(N):
minimumPrice = min(minimumPrice,
A[i] + cheapestPossible[i])
return minimumPrice
# Driver Code
if __name__ == "__main__":
A = [34, 12, 45, 10, 86, 39, 77]
B = [5, 42, 29, 63, 30, 33, 20]
N = len(A)
# Function Call
print(minimumCost(A, B, N))
# This code is contributed by ukasp.
C#
// C# program for above approach
using System;
using System.Collections.Generic;
public class GFG {
// Function to calculate minimum sum
public static int minimumCost(int[] A,
int[] B,
int N)
{
// For storing the minimum value
// from rear end of []B
int[] cheapestPossible = new int[N];
// Only one possible value
// on last index
cheapestPossible[N - 1]
= B[N - 1];
for (int i = (N - 2); i >= 0; i--) {
// The lowest possible sum at
// index i and above
cheapestPossible[i]
= Math.Min(cheapestPossible[i + 1],
B[i]);
}
// For storing minimum sum
int minimumPrice = int.MaxValue;
// Adding the current value of []A
// and minimum value of []B after i
// and comparing it with
// the last minimum sum obtained
for (int i = 0; i < N; i++) {
minimumPrice = Math.Min(
minimumPrice, A[i]
+ cheapestPossible[i]);
}
return minimumPrice;
}
// Driver code
public static void Main(String[] args)
{
int[] A = { 34, 12, 45, 10, 86, 39, 77 };
int[] B = { 5, 42, 29, 63, 30, 33, 20 };
int N = A.Length;
Console.WriteLine(minimumCost(A, B, N));
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
30
时间复杂度: O(N 2 )
辅助空间: O(1)
有效方法:解决此问题的一种有效方法是创建一个数组,该数组包含从B[]后端到第 i 个索引的最小值。然后遍历A[]并且对于每个索引,最小总和将是从A[]开始的最小值和从B[]后面到该索引的最小值的总和。请按照以下步骤解决给定的问题。
- 创建一个数组 用于存储每个索引的B[]后端的最小值。
- 由于最后一个索引只有一个可能的值,因此将数组B[]的最后一个索引值分配给新数组的最后一个索引。
- 现在,从后面遍历B[]并将 B[ ]中的当前索引(比如 i)值与新数组中的索引(i+1)进行比较,并将这两者的最小值存储在第 i 个索引中新创建的数组。
- 现在,遍历X的元素并查看新数组中的索引。因为新数组的索引是B[]中i及以上的最小可能值。
下面是上述有效方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to calculate minimum sum
int minimumCost(int A[], int B[], int N)
{
// For storing the minimum value
// from rear end of B[]
int cheapestPossible[N];
// Only one possible value on last index
cheapestPossible[N - 1] = B[N - 1];
for (int i = (N - 2); i >= 0; i--) {
// The lowest possible sum at
// index i and above
cheapestPossible[i]
= min(cheapestPossible[i + 1],
B[i]);
}
// For storing minimum sum
int minimumPrice = INT_MAX;
// Adding the current value of A[] and
// minimum value of B[] after i and
// comparing it with the last minimum sum
for (int i = 0; i < N; i++) {
minimumPrice
= min(minimumPrice,
A[i] + cheapestPossible[i]);
}
return minimumPrice;
}
// Driver Code
int main()
{
int A[] = { 34, 12, 45, 10, 86, 39, 77 };
int B[] = { 5, 42, 29, 63, 30, 33, 20 };
int N = sizeof(A) / sizeof(A[0]);
// Function Call
cout << minimumCost(A, B, N);
return 0;
}
Java
// Java program for above approach
import java.io.*;
class GFG {
// Function to calculate minimum sum
public static int minimumCost(int[] A,
int[] B,
int N)
{
// For storing the minimum value
// from rear end of B[]
int[] cheapestPossible = new int[N];
// Only one possible value
// on last index
cheapestPossible[N - 1]
= B[N - 1];
for (int i = (N - 2); i >= 0; i--) {
// The lowest possible sum at
// index i and above
cheapestPossible[i]
= Math.min(cheapestPossible[i + 1],
B[i]);
}
// For storing minimum sum
int minimumPrice = Integer.MAX_VALUE;
// Adding the current value of A[]
// and minimum value of B[] after i
// and comparing it with
// the last minimum sum obtained
for (int i = 0; i < N; i++) {
minimumPrice = Math.min(
minimumPrice, A[i]
+ cheapestPossible[i]);
}
return minimumPrice;
}
// Driver code
public static void main(String[] args)
{
int[] A = { 34, 12, 45, 10, 86, 39, 77 };
int[] B = { 5, 42, 29, 63, 30, 33, 20 };
int N = A.length;
System.out.println(minimumCost(A, B, N));
}
}
Python3
# Python 3 program for above approach
import sys
# Function to calculate minimum sum
def minimumCost(A, B, N):
# For storing the minimum value
# from rear end of B[]
cheapestPossible = [0]*N
# Only one possible value on last index
cheapestPossible[N - 1] = B[N - 1]
for i in range(N - 2 ,- 1, -1):
# The lowest possible sum at
# index i and above
cheapestPossible[i] = min(cheapestPossible[i + 1],
B[i])
# For storing minimum sum
minimumPrice = sys.maxsize
# Adding the current value of A[] and
# minimum value of B[] after i and
# comparing it with the last minimum sum
for i in range(N):
minimumPrice = min(minimumPrice,
A[i] + cheapestPossible[i])
return minimumPrice
# Driver Code
if __name__ == "__main__":
A = [34, 12, 45, 10, 86, 39, 77]
B = [5, 42, 29, 63, 30, 33, 20]
N = len(A)
# Function Call
print(minimumCost(A, B, N))
# This code is contributed by ukasp.
C#
// C# program for above approach
using System;
using System.Collections.Generic;
public class GFG {
// Function to calculate minimum sum
public static int minimumCost(int[] A,
int[] B,
int N)
{
// For storing the minimum value
// from rear end of []B
int[] cheapestPossible = new int[N];
// Only one possible value
// on last index
cheapestPossible[N - 1]
= B[N - 1];
for (int i = (N - 2); i >= 0; i--) {
// The lowest possible sum at
// index i and above
cheapestPossible[i]
= Math.Min(cheapestPossible[i + 1],
B[i]);
}
// For storing minimum sum
int minimumPrice = int.MaxValue;
// Adding the current value of []A
// and minimum value of []B after i
// and comparing it with
// the last minimum sum obtained
for (int i = 0; i < N; i++) {
minimumPrice = Math.Min(
minimumPrice, A[i]
+ cheapestPossible[i]);
}
return minimumPrice;
}
// Driver code
public static void Main(String[] args)
{
int[] A = { 34, 12, 45, 10, 86, 39, 77 };
int[] B = { 5, 42, 29, 63, 30, 33, 20 };
int N = A.Length;
Console.WriteLine(minimumCost(A, B, N));
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
30
时间复杂度: 在)
辅助空间: 在)