📌  相关文章
📜  最大化数组中连续减少的子序列的数量

📅  最后修改于: 2021-04-21 22:47:13             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是从满足以下条件的数组中找到可能递减的子序列的最大计数:

  • 每个子序列都以其最长的形式出现。
  • 子序列的相邻元素之间的差始终为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.


输出
3