📌  相关文章
📜  给定数组中三元组的最小可能价格总和

📅  最后修改于: 2021-09-17 06:51:25             🧑  作者: Mango

给定一个由N 个整数组成的数组 num[] ,其中每个元素都与另一个数组price[]给出的价格相关联,任务是通过取一个三元组来最小化价格总和,使得num[i] < num[j] <数量[k] 。如果没有这样的三元组,则打印 -1。

例子:

天真的方法:
最简单的方法是生成所有可能的三元组(i, j, k) ,使得i < j < knum[i] < num[j] < num[k]然后找到价格 [i]、价格 [ j]价格 [k] 。打印所有这些三元组的最小总和。

时间复杂度: O(N 3 )
辅助空间: O(1)

有效的方法:想法是使用辅助数组dp[]来存储所有这些三元组的最小价格总和,并打印存储在其中的所有价格的最小值。以下是步骤:

  1. dp[]数组初始化为INT_MAX
  2. 将当前最小总和(比如current_sum )初始化为INT_MAX
  3. 生成所有可能的对(i, j)使得j > i 。如果nums[j] > num[i]然后更新dp[j] = min(dp[j], price[i] + price[j])因为这是可能的对之一。
  4. 在上述步骤中的每一对(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


Javascript


输出:
50

时间复杂度: O(N 2 )
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程