相邻元素之差小于 D 的最长非递减子序列
给定一个包含N个整数的数组arr[]和一个整数D ,任务是找到最长的非递减子序列的长度,使得每个相邻元素之间的差小于D 。
例子:
Input: arr[] = {1, 3, 2, 4, 5}, D = 2
Output: 3
Explanation:
Consider the subsequence as {3, 4, 5}, which is of maximum length = 3 satisfying the given criteria.
Input: arr[] = {1, 5, 3, 2, 7}, D = 2
Output: 2
方法:给定的问题是最长递增子序列的变体,其标准是相邻数组元素之间的差异小于D ,这个想法可以使用动态规划来实现。请按照以下步骤解决给定的问题:
- 初始化一个dp数组,其中dp[i]将存储包含第i个元素后非递减子序列的最大长度,使得每对相邻元素之间的差异小于D 。
- 将数组dp[]的所有值初始化为1 。
- 在范围[0, N]上迭代一个循环,并且在每次迭代中,我使用变量j在范围[0, i – 1]上遍历给定数组arr[]并且如果arr[j]的值至少为arr[i]并且它们之间的差小于D ,然后将dp[i]的值更新为dp[i]和(1 + dp[j])的最大值。
- 完成上述步骤后,打印数组dp[]的最大值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return the length of the
// longest non-decreasing subsequence
// having the difference as D for every
// adjacent elements
int longestSubsequence(vector arr,
int d)
{
// Store the size of array
int n = arr.size();
// Stores the maximum length of the
// subsequence after including the
// ith element
vector dp(n, 1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
// If it satisfies the
// given condition
if (arr[i] - d < arr[j]
and arr[i] >= arr[j]) {
// Update dp[i]
dp[i] = max(dp[i], dp[j] + 1);
}
}
}
// Maximum value in the dp
// table is the answer
return *max_element(
dp.begin(), dp.end());
}
// Driver Code
int main()
{
vector arr = { 1, 3, 2, 4, 5 };
int D = 2;
cout << longestSubsequence(arr, D);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG {
// Function to return the length of the
// longest non-decreasing subsequence
// having the difference as D for every
// adjacent elements
static int longestSubsequence(int []arr,
int d)
{
// Store the size of array
int n = arr.length;
// Stores the maximum length of the
// subsequence after including the
// ith element
int []dp = new int[n];
for(int i = 0; i < n ; i++)
dp[i] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
// If it satisfies the
// given condition
if (arr[i] - d < arr[j] && arr[i] >= arr[j]) {
// Update dp[i]
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
// Maximum value in the dp
// table is the answer
Arrays.sort(dp);
return dp[n - 1];
}
// Driver Code
public static void main (String[] args) {
int arr[] = { 1, 3, 2, 4, 5 };
int D = 2;
System.out.println(longestSubsequence(arr, D));
}
}
// This code is contributed by AnkThon
Python3
# python program for the above approach
# Function to return the length of the
# longest non-decreasing subsequence
# having the difference as D for every
# adjacent elements
def longestSubsequence(arr, d):
# Store the size of array
n = len(arr)
# Stores the maximum length of the
# subsequence after including the
# ith element
dp = [1 for _ in range(n)]
for i in range(0, n):
for j in range(0, i):
# If it satisfies the
# given condition
if (arr[i] - d < arr[j] and arr[i] >= arr[j]):
# Update dp[i]
dp[i] = max(dp[i], dp[j] + 1)
# Maximum value in the dp
# table is the answer
return max(dp)
# Driver Code
if __name__ == "__main__":
arr = [1, 3, 2, 4, 5]
D = 2
print(longestSubsequence(arr, D))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
public class GFG {
// Function to return the length of the
// longest non-decreasing subsequence
// having the difference as D for every
// adjacent elements
static int longestSubsequence(int []arr,
int d)
{
// Store the size of array
int n = arr.Length;
// Stores the maximum length of the
// subsequence after including the
// ith element
int []dp = new int[n];
for(int i = 0; i < n ; i++)
dp[i] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
// If it satisfies the
// given condition
if (arr[i] - d < arr[j] && arr[i] >= arr[j]) {
// Update dp[i]
dp[i] = Math.Max(dp[i], dp[j] + 1);
}
}
}
// Maximum value in the dp
// table is the answer
Array.Sort(dp);
return dp[n - 1];
}
// Driver Code
public static void Main (string[] args) {
int []arr = { 1, 3, 2, 4, 5 };
int D = 2;
Console.WriteLine(longestSubsequence(arr, D));
}
}
// This code is contributed by AnkThon
Javascript
输出:
3
时间复杂度: O(N 2 )
辅助空间: O(N)