给定一个由N 个整数组成的数组arr[] ,任务是从满足以下条件的数组中找到可能的递减子序列的最大计数:
- 每个子序列都以其最长的可能形式存在。
- 子序列的相邻元素之间的差值始终为1 。
例子:
Input: arr[] = {2, 1, 5, 4, 3}
Output: 2
Explanation:
Possible decreasing subsequences are { 5, 4, 3 } and { 2, 1 }.
Input: arr[] = {4, 5, 2, 1, 4}
Output: 3
Explanation:
Possible decreasing subsequences are { 4 }, { 5, 4} and { 2, 1}.
方法:
这个想法是使用 HashMap 来解决问题。请按照以下步骤操作:
- 维护一个HashMap来存储数组元素可能的子序列的数量,并维护一个maxSubsequences来计算可能的子序列的总数。
- 遍历数组,对于每个元素arr[i] ,通过在HashMap 中分配给arr[i]的计数,检查是否存在可以将arr[i]作为下一个元素的任何子序列。
- 如果存在,请执行以下操作:
- 将arr[i]指定为子序列的下一个元素。
- 减少分配给HashMap 中arr[i] 的计数,因为 arr[i] 作为下一个元素的可能子序列的数量减少了1 。
- 类似地,增加分配给HashMap中的arr[i] – 1 的计数,因为arr[i] – 1作为下一个元素的可能子序列的数量增加了1 。
- 否则,增加maxCount ,因为需要一个新的子序列并重复上述步骤来修改HashMap 。
- 遍历完数组后,打印maxCount的值。
C++
// C++ program to implememt
// the above approach
#include
using namespace std;
// Function to find the maximum number
// number of required subsequences
int maxSubsequences(int arr[], int n)
{
// HashMap to store number of
// arrows available with
// height of arrow as key
unordered_map m;
// Stores the maximum count
// of possible subsequences
int maxCount = 0;
// Stores the count of
// possible subsequences
int count;
for (int i = 0; i < n; i++) {
// Check if i-th element can be
// part of any of the previous
// subsequence
if (m.find(arr[i]) != m.end()) {
// Count of subsequences
// possible with arr[i] as
// the next element
count = m[arr[i]];
// If more than one such
// subsequence exists
if (count > 1) {
// Include arr[i] in a subsequence
m[arr[i]] = count - 1;
}
// Otherwise
else
m.erase(arr[i]);
// Increase count of subsequence possible
// with arr[i] - 1 as the next element
if (arr[i] - 1 > 0)
m[arr[i] - 1] += 1;
}
else {
// Start a new subsequence
maxCount++;
// Increase count of subsequence possible
// with arr[i] - 1 as the next element
if (arr[i] - 1 > 0)
m[arr[i] - 1] += 1;
}
}
// Return the answer
return maxCount;
}
// Driver Code
int main()
{
int n = 5;
int arr[] = { 4, 5, 2, 1, 4 };
cout << maxSubsequences(arr, n) << endl;
// This code is contributed by bolliranadheer
}
Java
// Java program to implememt
// the above approach
import java.util.*;
class GFG {
// Function to find the maximum number
// number of required subsequences
static int maxSubsequences(int arr[], int n)
{
// HashMap to store number of
// arrows available with
// height of arrow as key
HashMap map
= new HashMap<>();
// Stores the maximum count
// of possible subsequences
int maxCount = 0;
// Stores the count of
// possible subsequences
int count;
for (int i = 0; i < n; i++)
{
// Check if i-th element can be
// part of any of the previous
// subsequence
if (map.containsKey(arr[i]))
{
// Count of subsequences
// possible with arr[i] as
// the next element
count = map.get(arr[i]);
// If more than one such
// subsequence exists
if (count > 1)
{
// Include arr[i] in a subsequence
map.put(arr[i], count - 1);
}
// Otherwise
else
map.remove(arr[i]);
// Increase count of subsequence possible
// with arr[i] - 1 as the next element
if (arr[i] - 1 > 0)
map.put(arr[i] - 1,
map.getOrDefault(arr[i] - 1, 0) + 1);
}
else {
// Start a new subsequence
maxCount++;
// Increase count of subsequence possible
// with arr[i] - 1 as the next element
if (arr[i] - 1 > 0)
map.put(arr[i] - 1,
map.getOrDefault(arr[i] - 1, 0) + 1);
}
}
// Return the answer
return maxCount;
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
int arr[] = { 4, 5, 2, 1, 4 };
System.out.println(maxSubsequences(arr, n));
}
}
C#
// C# program to implememt
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the maximum number
// number of required subsequences
static int maxSubsequences(int []arr, int n)
{
// Dictionary to store number of
// arrows available with
// height of arrow as key
Dictionary map = new Dictionary();
// Stores the maximum count
// of possible subsequences
int maxCount = 0;
// Stores the count of
// possible subsequences
int count;
for(int i = 0; i < n; i++)
{
// Check if i-th element can be
// part of any of the previous
// subsequence
if (map.ContainsKey(arr[i]))
{
// Count of subsequences
// possible with arr[i] as
// the next element
count = map[arr[i]];
// If more than one such
// subsequence exists
if (count > 1)
{
// Include arr[i] in a subsequence
map.Add(arr[i], count - 1);
}
// Otherwise
else
map.Remove(arr[i]);
// Increase count of subsequence possible
// with arr[i] - 1 as the next element
if (arr[i] - 1 > 0)
if (map.ContainsKey(arr[i] - 1))
map[arr[i] - 1]++;
else
map.Add(arr[i] - 1, 1);
}
else
{
// Start a new subsequence
maxCount++;
// Increase count of subsequence possible
// with arr[i] - 1 as the next element
if (arr[i] - 1 > 0)
if (map.ContainsKey(arr[i] - 1))
map[arr[i] - 1]++;
else
map.Add(arr[i] - 1, 1);
}
}
// Return the answer
return maxCount;
}
// Driver Code
public static void Main(String[] args)
{
int n = 5;
int []arr = { 4, 5, 2, 1, 4 };
Console.WriteLine(maxSubsequences(arr, n));
}
}
// This code is contributed by Amit Katiyar
Python3
# Python program to implememt
# the above approach
from collections import defaultdict
# Function to find the maximum number
# number of required subsequences
def maxSubsequences(arr, n)->int:
# Dictionary to store number of
# arrows available with
# height of arrow as key
m = defaultdict(int)
# Stores the maximum count
# of possible subsequences
maxCount = 0
# Stores the count
# of possible subsequences
count = 0
for i in range(0, n):
# Check if i-th element can be
# part of any of the previous
# subsequence
if arr[i] in m.keys():
# Count of subsequences
# possible with arr[i] as
# the next element
count = m[arr[i]]
# If more than one such
# subsequence exists
if count > 1:
# Include arr[i] in a subsequence
m[arr[i]] = count - 1
# Otherwise
else:
m.pop(arr[i])
# Increase count of subsequence possible
# with arr[i] - 1 as the next element
if arr[i] - 1 > 0:
m[arr[i] - 1] += 1
else:
maxCount += 1
# Increase count of subsequence possible
# with arr[i] - 1 as the next element
if arr[i] - 1 > 0:
m[arr[i] - 1] += 1
# Return the answer
return maxCount
# Driver Code
if __name__ == '__main__':
n = 5
arr = [4, 5, 2, 1, 4]
print(maxSubsequences(arr, n))
# This code is contributed by Riddhi Jaiswal.
Javascript
输出
3
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。