给定一个由N个正整数组成的数组arr [] ,任务是最大程度地增加可从数组中获得的子序列的数量,以使作为任何子序列一部分的每个元素arr [i]不超过该子序列的长度。
例子:
Input: arr[] = {1, 1, 1, 1}
Output: 4
Explanation:
Form 4 subsequences of 1 which satisfy the given condition {1}, {1}, {1}, {1}.
Input: arr[] = {2, 2, 3, 1, 2, 1}
Output: 3
Explanation:
The possible group are {1}, {1}, {2, 2}
So, the output is 3.
方法:想法是使用贪婪技术来解决此问题。请按照以下步骤解决问题:
- 初始化映射以存储每个数组元素的频率。
- 初始化一个变量,例如count为0,以存储获得的子序列总数。
- 跟踪剩余要添加的元素数量。
- 现在,遍历地图并计算可包含在特定组中的元素数量。
- 继续在有效子序列上添加元素。
- 完成上述步骤后,打印子序列的计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate the number
// of subsequences that can be formed
int No_Of_subsequences(map mp)
{
// Stores the number of subsequences
int count = 0;
int left = 0;
// Iterate over the map
for (auto x : mp) {
x.second += left;
// Count the number of subsequences
// that can be formed from x.first
count += (x.second / x.first);
// Number of occurrences of
// x.first which are left
left = x.second % x.first;
}
// Return the number of subsequences
return count;
}
// Function to create the maximum count
// of subsequences that can be formed
void maximumsubsequences(int arr[], int n)
{
// Stores the frequency of arr[]
map mp;
// Update the frequency
for (int i = 0; i < n; i++)
mp[arr[i]]++;
// Print the number of subsequences
cout << No_Of_subsequences(mp);
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 1, 1, 1, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
maximumsubsequences(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to calculate the number
// of subsequences that can be formed
public static int No_Of_subsequences(HashMap mp)
{
// Stores the number of subsequences
int count = 0;
int left = 0;
// Iterate over the map
for(Map.Entry x : mp.entrySet())
{
mp.replace(x.getKey(), x.getValue() + left);
// Count the number of subsequences
// that can be formed from x.first
count += (x.getValue() / x.getKey());
// Number of occurrences of
// x.first which are left
left = x.getValue() % x.getKey();
}
// Return the number of subsequences
return count;
}
// Function to create the maximum count
// of subsequences that can be formed
public static void maximumsubsequences(int[] arr,
int n)
{
// Stores the frequency of arr[]
HashMap mp = new HashMap<>();
// Update the frequency
for(int i = 0; i < n; i++)
{
if (mp.containsKey(arr[i]))
{
mp.replace(arr[i], mp.get(arr[i]) + 1);
}
else
{
mp.put(arr[i], 1);
}
}
// Print the number of subsequences
System.out.println(No_Of_subsequences(mp));
}
// Driver code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 1, 1, 1, 1 };
int N = arr.length;
// Function call
maximumsubsequences(arr, N);
}
}
// This code is contributed divyeshrabadiya07
Python3
# Python3 program for
# the above approach
from collections import defaultdict
# Function to calculate
# the number of subsequences
# that can be formed
def No_Of_subsequences(mp):
# Stores the number
# of subsequences
count = 0
left = 0
# Iterate over the map
for x in mp:
mp[x] += left
# Count the number of
# subsequences that can
# be formed from x.first
count += (mp[x] // x)
# Number of occurrences of
# x.first which are left
left = mp[x] % x
# Return the number
# of subsequences
return count
# Function to create the
# maximum count of subsequences
# that can be formed
def maximumsubsequences(arr, n):
# Stores the frequency of arr[]
mp = defaultdict (int)
# Update the frequency
for i in range (n):
mp[arr[i]] += 1
# Print the number of subsequences
print(No_Of_subsequences(mp))
# Driver Code
if __name__ == "__main__":
# Given array arr[]
arr = [1, 1, 1, 1]
N = len(arr)
# Function Call
maximumsubsequences(arr, N)
# This code is contributed by Chitranayal
C#
// C# program for
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to calculate the number
// of subsequences that can be formed
public static int No_Of_subsequences(Dictionary mp)
{
// Stores the number
// of subsequences
int count = 0;
int left = 0;
// Iterate over the map
foreach(KeyValuePair x in mp)
{
if(!mp.ContainsKey(x.Key))
mp.Add(x.Key, x.Value + left);
// Count the number of subsequences
// that can be formed from x.first
count += (x.Value / x.Key);
// Number of occurrences of
// x.first which are left
left = x.Value % x.Key;
}
// Return the number of subsequences
return count;
}
// Function to create the maximum count
// of subsequences that can be formed
public static void maximumsubsequences(int[] arr,
int n)
{
// Stores the frequency of []arr
Dictionary mp = new Dictionary();
// Update the frequency
for(int i = 0; i < n; i++)
{
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]] = mp[arr[i]] + 1;
}
else
{
mp.Add(arr[i], 1);
}
}
// Print the number of subsequences
Console.WriteLine(No_Of_subsequences(mp));
}
// Driver code
public static void Main(String[] args)
{
// Given array []arr
int []arr = {1, 1, 1, 1};
int N = arr.Length;
// Function call
maximumsubsequences(arr, N);
}
}
// This code is contributed by Rajput-Ji
输出:
4
时间复杂度: O(N * log N)
辅助空间: O(N)