根据给定条件,从整数对数组中最大化选定整数的总和
给定一个数组arr[]具有N对形式为(x, y)的整数,任务是最大化选定对中的总和y值,使得如果选择一对(x i , y i ) ,则下一个x无法选择i对。
例子:
Input: arr[]= {{1, 5}, {2, 7}, {1, 4}, {1, 5}, {1, 10}}
Output: 19
Explanation: Choose the pair at the index i = 0 i.e, (1, 5). Hence the next 1 pair cannot be selected. Similarly, select the pairs at index 2 and 4 i.e, (1, 4) and (1, 10). Therefore, the sum of all the y values of the selected pairs is 5+4+10 = 19, which is the maximum possible.
Input: arr[] = {{1, 5}, {2, 10}, {3, 3}, {7, 4}}
Output: 10
方法:给定的问题可以使用动态规划来解决。创建一个数组dp[] ,其中dp[i]表示[i, N)范围内数组arr[]的选定元素的最大可能总和,并将所有索引的值初始化为0 。因此,可以使用以下关系计算dp[]数组:
dp[i] = max( dp[i + 1], dp[i + xi + 1] + yi)
因此,为(N, 0]范围内的所有i值计算dp[i]中每个状态的值。存储在dp[0]中的值将是所需的答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to maximize sum of selected
// integers from an array of pair
// of integers as per given condition
int maximizeSum(vector > arr, int N)
{
// Stores the DP states
vector dp(N + 1, 0);
// Initial Condition
dp[N - 1] = arr[N - 1].second;
// Loop to traverse the array
// in the range (N - 1, 0]
for (int i = N - 2; i >= 0; i--) {
// If it is in range
if (i + arr[i].first < N)
// Calculate dp[i]
dp[i] = max(dp[i + 1],
dp[i + arr[i].first + 1]
+ arr[i].second);
else
dp[i] = dp[i + 1];
}
// Return Answer
return dp[0];
}
// Driver Code
int main()
{
vector > arr = {
{ 1, 5 }, { 2, 7 }, { 1, 4 }, { 1, 5 }, { 1, 10 }
};
cout << maximizeSum(arr, arr.size());
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
// User defined Pair class
class Pair {
int first;
int second;
// Constructor
public Pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
class GFG {
// Function to maximize sum of selected
// integers from an array of pair
// of integers as per given condition
static int maximizeSum(Pair arr[ ], int N)
{
// Stores the DP states
int dp[] = new int[N + 1];
// Initial Condition
dp[N - 1] = arr[N - 1].second;
// Loop to traverse the array
// in the range (N - 1, 0]
for (int i = N - 2; i >= 0; i--) {
// If it is in range
if (i + arr[i].first < N)
// Calculate dp[i]
dp[i] = Math.max(dp[i + 1],
dp[i + arr[i].first + 1]
+ arr[i].second);
else
dp[i] = dp[i + 1];
}
// Return Answer
return dp[0];
}
// Driver Code
public static void main (String[] args)
{
int n = 5;
// Array of Pair
Pair arr[] = new Pair[n];
arr[0] = new Pair(1, 5);
arr[1] = new Pair(2, 7);
arr[2] = new Pair(1, 4);
arr[3] = new Pair(1, 5);
arr[4] = new Pair(1, 10);
System.out.print(maximizeSum(arr, n));
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python code for the above approach
# Function to maximize sum of selected
# integers from an array of pair
# of integers as per given condition
def maximizeSum(arr, N):
# Stores the DP states
dp = [0] * (N + 1)
# Initial Condition
dp[N - 1] = arr[N - 1]["second"]
# Loop to traverse the array
# in the range (N - 1, 0]
for i in range(N - 2, -1, -1):
# If it is in range
if (i + arr[i]["first"] < N):
# Calculate dp[i]
dp[i] = max(dp[i + 1],
dp[i + arr[i]["first"] + 1]
+ arr[i]["second"])
else:
dp[i] = dp[i + 1]
# Return Answer
return dp[0]
# Driver Code
arr = [{"first": 1, "second": 5},
{"first": 2, "second": 7},
{"first": 1, "second": 4},
{"first": 1, "second": 5},
{"first": 1, "second": 10}
]
print(maximizeSum(arr, len(arr)))
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
// User defined Pair class
class Pair {
public int first;
public int second;
// Constructor
public Pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
public class GFG {
// Function to maximize sum of selected
// integers from an array of pair
// of integers as per given condition
static int maximizeSum(Pair []arr, int N)
{
// Stores the DP states
int []dp = new int[N + 1];
// Initial Condition
dp[N - 1] = arr[N - 1].second;
// Loop to traverse the array
// in the range (N - 1, 0]
for (int i = N - 2; i >= 0; i--) {
// If it is in range
if (i + arr[i].first < N)
// Calculate dp[i]
dp[i] = Math.Max(dp[i + 1],
dp[i + arr[i].first + 1]
+ arr[i].second);
else
dp[i] = dp[i + 1];
}
// Return Answer
return dp[0];
}
// Driver Code
public static void Main(String[] args)
{
int n = 5;
// Array of Pair
Pair []arr = new Pair[n];
arr[0] = new Pair(1, 5);
arr[1] = new Pair(2, 7);
arr[2] = new Pair(1, 4);
arr[3] = new Pair(1, 5);
arr[4] = new Pair(1, 10);
Console.Write(maximizeSum(arr, n));
}
}
// This code is contributed by shikhasingrajput
Javascript
19
时间复杂度: O(N)
辅助空间: O(N)