给定一个由N个整数组成的数组num [] ,其中每个元素与另一个数组price []给出的价格相关联,任务是通过采用三元组使num [i]
例子:
Input: num[]={2, 4, 6, 7, 8}, price[]={10, 20, 100, 20, 40}
Output: 50
Explanation:
Selecting the triplet {2, 4, 7} because (2 < 4 < 7), and the price is 10 + 20 + 20 = 50 which is the minimum possible.
Input: num[]={100, 101, 100}, price[]={2, 4, 5}
Output: -1
Explanation:
No possible triplet exists.
天真的方法:
最简单的方法是生成所有可能的三元组(i,j,k) ,以使i
时间复杂度: O(N 3 )
辅助空间: O(1)
高效方法:想法是使用辅助数组dp []存储所有这些三元组的最小价格总和,并打印存储在其中的所有价格中的最小价格。步骤如下:
- 将dp []数组初始化为INT_MAX 。
- 将当前的最小和(例如current_sum )初始化为INT_MAX 。
- 生成所有可能的对(i,j) ,使得j> i 。如果nums [j]> num [i],则更新dp [j] = min(dp [j],price [i] + price [j]),因为这是可能的对之一。
- 在上述步骤中的每对(i,j)中,将三元组的最小总和更新为min(current_sum,dp [i] + price [j]) 。该步骤将确保形成可能的三元组(i,j,k) ,因为dp [i]将存储索引i和j处的价格之和,并且j是k的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
#include
using namespace std;
// Function to minimize the sum of
// price by taking a triplet
long minSum(int n, int num[], int price[])
{
// Initialize a dp[] array
long dp[n];
for(int i = 0; i < n; i++)
dp[i] = INT_MAX;
// Stores the final result
long ans = INT_MAX;
// Iterate for all values till N
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
// Check if num[j] > num[i]
if (num[j] > num[i])
{
// Update dp[j] if it is
// greater than stored value
dp[j] = (long)min((long)dp[j],
(long)price[i] +
(long)price[j]);
// Update the minimum
// sum as ans
ans = min(ans, (long)dp[i] +
(long)price[j]);
}
}
}
// If there is no minimum sum exist
// then print -1 else print the ans
return ans != INT_MAX ? ans : -1;
}
// Driver Code
int main()
{
int num[] = { 2, 4, 6, 7, 8 };
int price[] = { 10, 20, 100, 20, 40 };
int n = sizeof(price) / sizeof(price[0]);
cout << (minSum(n, num, price));
}
// This code is contributed by chitranayal
Java
// Java Program to implement
// the above approach
import java.util.*;
import java.io.*;
public class Main {
// Function to minimize the sum of
// price by taking a triplet
public static long minSum(int n, int num[],
int price[])
{
// Initialize a dp[] array
long dp[] = new long[n];
Arrays.fill(dp, Integer.MAX_VALUE);
// Stores the final result
long ans = Integer.MAX_VALUE;
// Iterate for all values till N
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// Check if num[j] > num[i]
if (num[j] > num[i]) {
// Update dp[j] if it is
// greater than stored value
dp[j] = (long)Math.min(
(long)dp[j],
(long)price[i]
+ (long)price[j]);
// Update the minimum
// sum as ans
ans = Math.min(
ans, (long)dp[i]
+ (long)price[j]);
}
}
}
// If there is no minimum sum exist
// then print -1 else print the ans
return ans != Integer.MAX_VALUE ? ans : -1;
}
// Driver Code
public static void
main(String[] args)
{
int num[] = { 2, 4, 6, 7, 8 };
int price[] = { 10, 20, 100, 20, 40 };
int n = price.length;
System.out.println(minSum(n, num, price));
}
}
Python3
# Python3 program to implement
# the above approach
import sys;
# Function to minimize the sum of
# price by taking a triplet
def minSum(n, num, price):
# Initialize a dp[] list
dp = [0 for i in range(n)]
for i in range(n):
dp[i] = sys.maxsize
# Stores the final result
ans = sys.maxsize
# Iterate for all values till N
for i in range(n):
for j in range(i + 1, n):
# Check if num[j] > num[i]
if (num[j] > num[i]):
# Update dp[j] if it is
# greater than stored value
dp[j] = min(dp[j], price[i] +
price[j])
# Update the minimum
# sum as ans
ans = min(ans, dp[i] + price[j])
# If there is no minimum sum exist
# then print -1 else print the ans
if ans is not sys.maxsize:
return ans
else:
return -1
# Driver code
if __name__=='__main__':
num = [ 2, 4, 6, 7, 8 ]
price = [ 10, 20, 100, 20, 40 ]
n = len(price)
print(minSum(n, num, price))
# This code is contributed by rutvik_56
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to minimize the sum of
// price by taking a triplet
public static long minSum(int n, int []num,
int []price)
{
// Initialize a []dp array
long []dp = new long[n];
for(int i = 0; i < n; i++)
dp[i] = int.MaxValue;
// Stores the readonly result
long ans = int.MaxValue;
// Iterate for all values till N
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
// Check if num[j] > num[i]
if (num[j] > num[i])
{
// Update dp[j] if it is
// greater than stored value
dp[j] = (long)Math.Min((long)dp[j],
(long)price[i] +
(long)price[j]);
// Update the minimum
// sum as ans
ans = Math.Min(ans, (long)dp[i] +
(long)price[j]);
}
}
}
// If there is no minimum sum exist
// then print -1 else print the ans
return ans != int.MaxValue ? ans : -1;
}
// Driver Code
public static void Main(String[] args)
{
int []num = { 2, 4, 6, 7, 8 };
int []price = { 10, 20, 100, 20, 40 };
int n = price.Length;
Console.WriteLine(minSum(n, num, price));
}
}
// This code is contributed by 29AjayKumar
输出:
50
时间复杂度: O(N 2 )
辅助空间: O(N)