给定两个大小为N的数组arr []和brr [] ,使得数组brr []由与数组arr []的相应元素相关的分数组成。任务是找到数组arr []的数字连续且不同元素的子序列的分配分数的最大可能总和。
例子:
Input: arr[] = {1, 2, 3, 3, 3, 1}, brr[] = {-1, 2, 10, 20, -10, -9}
Output: 22
Explanation:
Distinct values from the array = {1, 2, 3}
Maximum value assigned to each element = {1: -1, 2: 2, 3: 20}
Select the elements at index 2 and 4 in arr[] which are {2, 3}.
Maximum score = 2 + 20 = 22.
Input: arr[] = {1, 2, 3, 2, 3, 1}, brr[] = {-1, 2, 10, 20, -10, -9}
Output: 32
Explanation: Selected subsequence is {arr[1], arr[2], arr[3]} = {2, 3, 2}
天真的方法:解决问题的最简单方法是使用递归。请按照以下步骤解决问题:
- 生成给定数组arr []的所有可能子集,以使子集具有唯一且连续的元素。
- 在上述步骤中生成子集时,每个元素都有两种可能性被添加到子序列中或不被添加到子序列中。因此,请按照下列步骤操作:
- 如果当前元素与先前选择的元素相差1,则将该元素添加到子序列中。
- 否则,请继续执行下一个元素。
- 通过同时考虑以上两种可能性来更新maximum_score 。
- 打印完成数组遍历后获得的maximum_score最终值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum score
// with unique element in the subset
int maximumSum(int a[], int b[], int n,
int index, int lastpicked)
{
// Base Case
if (index == n)
return 0;
int option1 = 0, option2 = 0;
// Check if the previously picked
// element differs by 1 from the
// current element
if (lastpicked == -1 ||
a[lastpicked] != a[index])
// Calculate score by including
// the current element
option1 = b[index] + maximumSum(
a, b, n,
index + 1,
index);
// Calculate score by excluding
// the current element
option2 = maximumSum(a, b, n,
index + 1,
lastpicked);
// Return maximum of the
// two possibilities
return max(option1, option2);
}
// Driver code
int main()
{
// Given arrays
int arr[] = { 1, 2, 3, 3, 3, 1 };
int brr[] = { -1, 2, 10, 20, -10, -9 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << (maximumSum(arr, brr, N, 0, -1));
}
// This code is contributed by rutvik_56
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find the maximum score
// with unique element in the subset
public static int maximumSum(
int[] a, int[] b, int n, int index,
int lastpicked)
{
// Base Case
if (index == n)
return 0;
int option1 = 0, option2 = 0;
// Check if the previously picked
// element differs by 1 from the
// current element
if (lastpicked == -1
|| a[lastpicked] != a[index])
// Calculate score by including
// the current element
option1
= b[index]
+ maximumSum(a, b, n,
index + 1,
index);
// Calculate score by excluding
// the current element
option2 = maximumSum(a, b, n,
index + 1,
lastpicked);
// Return maximum of the
// two possibilities
return Math.max(option1, option2);
}
// Driver Code
public static void main(String[] args)
{
// Given arrays
int arr[] = { 1, 2, 3, 3, 3, 1 };
int brr[] = { -1, 2, 10, 20, -10, -9 };
int N = arr.length;
// Function Call
System.out.println(
maximumSum(arr, brr,
N, 0, -1));
}
}
Python3
# Python3 program for the above approach
# Function to find the maximum score
# with unique element in the subset
def maximumSum(a, b, n, index, lastpicked):
# Base Case
if (index == n):
return 0
option1 = 0
option2 = 0
# Check if the previously picked
# element differs by 1 from the
# current element
if (lastpicked == -1 or
a[lastpicked] != a[index]):
# Calculate score by including
# the current element
option1 = b[index] + maximumSum(a, b, n,
index + 1,
index)
# Calculate score by excluding
# the current element
option2 = maximumSum(a, b, n, index + 1,
lastpicked)
# Return maximum of the
# two possibilities
return max(option1, option2)
# Driver Code
if __name__ == '__main__':
# Given arrays
arr = [ 1, 2, 3, 3, 3, 1 ]
brr = [ -1, 2, 10, 20, -10, -9 ]
N = len(arr)
# Function call
print(maximumSum(arr, brr, N, 0, -1))
# This code is contributed by mohit kumar 29
C#
// C# program for
// the above approach
using System;
class GFG{
// Function to find the maximum score
// with unique element in the subset
public static int maximumSum(int[] a, int[] b,
int n, int index,
int lastpicked)
{
// Base Case
if (index == n)
return 0;
int option1 = 0, option2 = 0;
// Check if the previously picked
// element differs by 1 from the
// current element
if (lastpicked == -1 ||
a[lastpicked] != a[index])
// Calculate score by including
// the current element
option1 = b[index] + maximumSum(a, b, n,
index + 1,
index);
// Calculate score by excluding
// the current element
option2 = maximumSum(a, b, n,
index + 1,
lastpicked);
// Return maximum of the
// two possibilities
return Math.Max(option1, option2);
}
// Driver Code
public static void Main(String[] args)
{
// Given arrays
int []arr = {1, 2, 3, 3, 3, 1};
int []brr = {-1, 2, 10, 20, -10, -9};
int N = arr.Length;
// Function Call
Console.WriteLine(maximumSum(arr, brr,
N, 0, -1));
}
}
// This code is contributed by shikhasingrajput
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum
// score possible
int maximumSum(int a[], int b[], int n,
int index, int lastpicked,
vector> dp)
{
// Base Case
if (index == n)
return 0;
// If previously occurred subproblem
// occurred
if (dp[index][lastpicked + 1] != -1)
return dp[index][lastpicked + 1];
int option1 = 0, option2 = 0;
// Check if lastpicked element differs
// by 1 from the current element
if (lastpicked == -1 ||
a[lastpicked] != a[index])
{
// Calculate score by including
// the current element
option1 = b[index] + maximumSum(a, b, n,
index + 1,
index, dp);
}
// Calculate score by excluding
// the current element
option2 = maximumSum(a, b, n,
index + 1,
lastpicked, dp);
// Return maximum score from
// the two possibilities
return dp[index][lastpicked + 1] = max(option1,
option2);
}
// Function to print maximum score
void maximumPoints(int arr[], int brr[], int n)
{
int index = 0, lastPicked = -1;
// DP array to store results
// Initialise dp with -1
vector> dp(n + 5, vector(n + 5, -1));
// Function call
cout << maximumSum(arr, brr, n, index,
lastPicked, dp)
<< endl;
}
// Driver code
int main()
{
// Given arrays
int arr[] = { 1, 2, 3, 3, 3, 1 };
int brr[] = { -1, 2, 10, 20, -10, -9 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
maximumPoints(arr, brr, N);
return 0;
}
// This code is contributed by divyeshrabadiya07
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the maximum
// score possible
public static int maximumSum(
int[] a, int[] b, int n, int index,
int lastpicked, int[][] dp)
{
// Base Case
if (index == n)
return 0;
// If previously occurred subproblem
// occurred
if (dp[index][lastpicked + 1] != -1)
return dp[index][lastpicked + 1];
int option1 = 0, option2 = 0;
// Check if lastpicked element differs
// by 1 from the current element
if (lastpicked == -1
|| a[lastpicked] != a[index]) {
// Calculate score by including
// the current element
option1 = b[index]
+ maximumSum(a, b, n,
index + 1,
index, dp);
}
// Calculate score by excluding
// the current element
option2 = maximumSum(a, b, n,
index + 1,
lastpicked, dp);
// Return maximum score from
// the two possibilities
return dp[index][lastpicked + 1]
= Math.max(option1, option2);
}
// Function to print maximum score
public static void maximumPoints(
int arr[], int brr[], int n)
{
int index = 0, lastPicked = -1;
// DP array to store results
int dp[][] = new int[n + 5][n + 5];
// Initialise dp with -1
for (int i[] : dp)
Arrays.fill(i, -1);
// Function call
System.out.println(
maximumSum(arr, brr, n,
index, lastPicked, dp));
}
// Driver Code
public static void main(String[] args)
{
// Given arrays
int arr[] = { 1, 2, 3, 3, 3, 1 };
int brr[] = { -1, 2, 10, 20, -10, -9 };
int N = arr.length;
// Function Call
maximumPoints(arr, brr, N);
}
}
Python3
# Python3 program for
# the above approach
# Function to find the
# maximum score possible
def maximumSum(a, b, n, index,
lastpicked, dp):
# Base Case
if (index == n):
return 0
# If previously occurred
# subproblem occurred
if (dp[index][lastpicked + 1] != -1):
return dp[index][lastpicked + 1]
option1, option2 = 0, 0
# Check if lastpicked element differs
# by 1 from the current element
if (lastpicked == -1 or
a[lastpicked] != a[index]):
# Calculate score by including
# the current element
option1 = (b[index] +
maximumSum(a, b, n,
index + 1,
index, dp))
# Calculate score by excluding
# the current element
option2 = maximumSum(a, b, n,
index + 1,
lastpicked, dp)
# Return maximum score from
# the two possibilities
dp[index][lastpicked + 1] = max(option1,
option2)
return dp[index][lastpicked + 1]
# Function to print maximum score
def maximumPoints(arr, brr, n):
index = 0
lastPicked = -1
# DP array to store results
dp =[[ -1 for x in range (n + 5)]
for y in range (n + 5)]
# Function call
print (maximumSum(arr, brr,
n, index,
lastPicked, dp))
# Driver Code
if __name__ == "__main__":
# Given arrays
arr = [1, 2, 3, 3, 3, 1]
brr = [-1, 2, 10, 20, -10, -9]
N = len(arr)
# Function Call
maximumPoints(arr, brr, N)
# This code is contributed by Chitranayal
C#
// C# program for
// the above approach
using System;
class GFG{
// Function to find the maximum
// score possible
public static int maximumSum(int[] a, int[] b,
int n, int index,
int lastpicked,
int[,] dp)
{
// Base Case
if (index == n)
return 0;
// If previously occurred
// subproblem occurred
if (dp[index, lastpicked + 1] != -1)
return dp[index, lastpicked + 1];
int option1 = 0, option2 = 0;
// Check if lastpicked element differs
// by 1 from the current element
if (lastpicked == -1 ||
a[lastpicked] != a[index])
{
// Calculate score by including
// the current element
option1 = b[index] + maximumSum(a, b, n,
index + 1,
index, dp);
}
// Calculate score by excluding
// the current element
option2 = maximumSum(a, b, n,
index + 1,
lastpicked, dp);
// Return maximum score from
// the two possibilities
return dp[index, lastpicked + 1] =
Math.Max(option1, option2);
}
// Function to print maximum score
public static void maximumPoints(int []arr,
int []brr,
int n)
{
int index = 0, lastPicked = -1;
// DP array to store results
int [,]dp = new int[n + 5, n + 5];
// Initialise dp with -1
for(int i = 0; i < n + 5; i++)
{
for (int j = 0; j < n + 5; j++)
{
dp[i, j] = -1;
}
}
// Function call
Console.WriteLine(maximumSum(arr, brr, n,
index, lastPicked,
dp));
}
// Driver Code
public static void Main(String[] args)
{
// Given arrays
int []arr = {1, 2, 3, 3, 3, 1};
int []brr = {-1, 2, 10, 20, -10, -9};
int N = arr.Length;
// Function Call
maximumPoints(arr, brr, N);
}
}
// This code is contributed by Rajput-Ji
22
时间复杂度: O(2 N )
辅助空间: O(1)
高效方法:由于问题存在子问题重叠,因此可以通过使用动态编程来优化上述方法。步骤如下:
- 将index初始化为0并将lastPicked初始化为-1 。
- 初始化一个二维数组,例如dp [] [],以存储子问题的结果。
- dp [] []的状态将是当前索引和最后选择的整数。
- 计算两个可能选项的分数:
- 如果最后选择的整数与当前整数不同,请选择当前元素。
- 跳过当前元素,移至下一个元素。
- 将当前状态存储为两个状态之上计算出的最大值。
- 在所有递归调用结束后,将dp [index] [lastPicked + 1]的值打印为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum
// score possible
int maximumSum(int a[], int b[], int n,
int index, int lastpicked,
vector> dp)
{
// Base Case
if (index == n)
return 0;
// If previously occurred subproblem
// occurred
if (dp[index][lastpicked + 1] != -1)
return dp[index][lastpicked + 1];
int option1 = 0, option2 = 0;
// Check if lastpicked element differs
// by 1 from the current element
if (lastpicked == -1 ||
a[lastpicked] != a[index])
{
// Calculate score by including
// the current element
option1 = b[index] + maximumSum(a, b, n,
index + 1,
index, dp);
}
// Calculate score by excluding
// the current element
option2 = maximumSum(a, b, n,
index + 1,
lastpicked, dp);
// Return maximum score from
// the two possibilities
return dp[index][lastpicked + 1] = max(option1,
option2);
}
// Function to print maximum score
void maximumPoints(int arr[], int brr[], int n)
{
int index = 0, lastPicked = -1;
// DP array to store results
// Initialise dp with -1
vector> dp(n + 5, vector(n + 5, -1));
// Function call
cout << maximumSum(arr, brr, n, index,
lastPicked, dp)
<< endl;
}
// Driver code
int main()
{
// Given arrays
int arr[] = { 1, 2, 3, 3, 3, 1 };
int brr[] = { -1, 2, 10, 20, -10, -9 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
maximumPoints(arr, brr, N);
return 0;
}
// This code is contributed by divyeshrabadiya07
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the maximum
// score possible
public static int maximumSum(
int[] a, int[] b, int n, int index,
int lastpicked, int[][] dp)
{
// Base Case
if (index == n)
return 0;
// If previously occurred subproblem
// occurred
if (dp[index][lastpicked + 1] != -1)
return dp[index][lastpicked + 1];
int option1 = 0, option2 = 0;
// Check if lastpicked element differs
// by 1 from the current element
if (lastpicked == -1
|| a[lastpicked] != a[index]) {
// Calculate score by including
// the current element
option1 = b[index]
+ maximumSum(a, b, n,
index + 1,
index, dp);
}
// Calculate score by excluding
// the current element
option2 = maximumSum(a, b, n,
index + 1,
lastpicked, dp);
// Return maximum score from
// the two possibilities
return dp[index][lastpicked + 1]
= Math.max(option1, option2);
}
// Function to print maximum score
public static void maximumPoints(
int arr[], int brr[], int n)
{
int index = 0, lastPicked = -1;
// DP array to store results
int dp[][] = new int[n + 5][n + 5];
// Initialise dp with -1
for (int i[] : dp)
Arrays.fill(i, -1);
// Function call
System.out.println(
maximumSum(arr, brr, n,
index, lastPicked, dp));
}
// Driver Code
public static void main(String[] args)
{
// Given arrays
int arr[] = { 1, 2, 3, 3, 3, 1 };
int brr[] = { -1, 2, 10, 20, -10, -9 };
int N = arr.length;
// Function Call
maximumPoints(arr, brr, N);
}
}
Python3
# Python3 program for
# the above approach
# Function to find the
# maximum score possible
def maximumSum(a, b, n, index,
lastpicked, dp):
# Base Case
if (index == n):
return 0
# If previously occurred
# subproblem occurred
if (dp[index][lastpicked + 1] != -1):
return dp[index][lastpicked + 1]
option1, option2 = 0, 0
# Check if lastpicked element differs
# by 1 from the current element
if (lastpicked == -1 or
a[lastpicked] != a[index]):
# Calculate score by including
# the current element
option1 = (b[index] +
maximumSum(a, b, n,
index + 1,
index, dp))
# Calculate score by excluding
# the current element
option2 = maximumSum(a, b, n,
index + 1,
lastpicked, dp)
# Return maximum score from
# the two possibilities
dp[index][lastpicked + 1] = max(option1,
option2)
return dp[index][lastpicked + 1]
# Function to print maximum score
def maximumPoints(arr, brr, n):
index = 0
lastPicked = -1
# DP array to store results
dp =[[ -1 for x in range (n + 5)]
for y in range (n + 5)]
# Function call
print (maximumSum(arr, brr,
n, index,
lastPicked, dp))
# Driver Code
if __name__ == "__main__":
# Given arrays
arr = [1, 2, 3, 3, 3, 1]
brr = [-1, 2, 10, 20, -10, -9]
N = len(arr)
# Function Call
maximumPoints(arr, brr, N)
# This code is contributed by Chitranayal
C#
// C# program for
// the above approach
using System;
class GFG{
// Function to find the maximum
// score possible
public static int maximumSum(int[] a, int[] b,
int n, int index,
int lastpicked,
int[,] dp)
{
// Base Case
if (index == n)
return 0;
// If previously occurred
// subproblem occurred
if (dp[index, lastpicked + 1] != -1)
return dp[index, lastpicked + 1];
int option1 = 0, option2 = 0;
// Check if lastpicked element differs
// by 1 from the current element
if (lastpicked == -1 ||
a[lastpicked] != a[index])
{
// Calculate score by including
// the current element
option1 = b[index] + maximumSum(a, b, n,
index + 1,
index, dp);
}
// Calculate score by excluding
// the current element
option2 = maximumSum(a, b, n,
index + 1,
lastpicked, dp);
// Return maximum score from
// the two possibilities
return dp[index, lastpicked + 1] =
Math.Max(option1, option2);
}
// Function to print maximum score
public static void maximumPoints(int []arr,
int []brr,
int n)
{
int index = 0, lastPicked = -1;
// DP array to store results
int [,]dp = new int[n + 5, n + 5];
// Initialise dp with -1
for(int i = 0; i < n + 5; i++)
{
for (int j = 0; j < n + 5; j++)
{
dp[i, j] = -1;
}
}
// Function call
Console.WriteLine(maximumSum(arr, brr, n,
index, lastPicked,
dp));
}
// Driver Code
public static void Main(String[] args)
{
// Given arrays
int []arr = {1, 2, 3, 3, 3, 1};
int []brr = {-1, 2, 10, 20, -10, -9};
int N = arr.Length;
// Function Call
maximumPoints(arr, brr, N);
}
}
// This code is contributed by Rajput-Ji
22
时间复杂度: O(N 2 )
辅助空间: O(N 2 )