给定一个由N个不同整数组成的数组。对于每个元素,任务是从所有最小子元素为当前元素的可能子序列中找到子序列的计数。
例子:
Input: arr[] = {1, 2}
Output: 2 1
Explanation:
Subsequences are {1}, {2}, {1, 2}.
The count of the smallest element in each subsequence is:
1 = 2, 2 = 1
Input: arr[] = {1, 2, 3}
Output: 4 2 1
天真的方法:想法是生成给定数组的所有可能的子序列,并对每个子序列中的最小元素进行计数,并为数组中的每个元素打印其计数。
时间复杂度: O(2 N )
辅助空间: O(N)
高效方法:这个想法是观察一个模式,即观察最小元素出现2 n – 1次,第二个最小元素出现2 n – 2次,依此类推…… 。例如:
Let the array be arr[] = {1, 2, 3}
Subsequences are {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}
Minimum of each subsequence: {1}, {2}, {3}, {1}, {1}, {2}, {1}.
where
1 occurs 4 times i.e. 2n – 1 where n = 3.
2 occurs 2 times i.e. 2n – 2 where n = 3.
3 occurs 1 times i.e. 2n – 3 where n = 3.
步骤如下:
- 将每个元素的索引存储在Map中,以便我们可以按照原始数组的顺序打印该元素。
- 对给定的数组进行排序。
- 现在元素按递增顺序排列,并且根据上述观察,遍历给定的数组并保留子序列数,以使每个元素都是最小的元素由pow(2,N – 1 – i)给出。
- 现在根据原始数组中的元素遍历子序列的映射和打印计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function that count the subsequence
// such that each element as the
// minimum element in the subsequence
void solve(int arr[], int N)
{
map M;
// Store index in a map
for (int i = 0; i < N; i++) {
M[i] = arr[i];
}
// Sort the array
sort(arr, arr + N);
// To store count of subsequence
unordered_map Count;
// Traverse the array
for (int i = 0; i < N; i++) {
// Store count of subsequence
Count[arr[i]] = pow(2, N - i - 1);
}
// Print the count of subsequence
for (auto& it : M) {
cout << Count[M[it.second]] << ' ';
}
}
// Driver code
int main()
{
int arr[] = { 5, 2, 1 };
int N = sizeof arr / sizeof arr[0];
// Function call
solve(arr, N);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function that count the subsequence
// such that each element as the
// minimum element in the subsequence
static void solve(int arr[], int N)
{
HashMap M = new HashMap<>();
// Store index in a map
for (int i = 0; i < N; i++)
{
M.put(i, arr[i]);
}
// Sort the array
Arrays.sort(arr);
// To store count of subsequence
HashMap Count = new HashMap<>();
// Traverse the array
for (int i = 0; i < N; i++)
{
// Store count of subsequence
Count.put(arr[i],
(int)Math.pow(2, N - i - 1));
}
// Print the count of subsequence
for (Map.Entry m : M.entrySet())
{
System.out.print(Count.get(m.getValue()) + " ");
}
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 5, 2, 1 };
int N = arr.length;
// Function call
solve(arr, N);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
# Function that count the subsequence
# such that each element as the
# minimum element in the subsequence
def solve(arr, N):
M = {}
# Store index in a map
for i in range(N):
M[i] = arr[i]
# Sort the array
arr.sort()
# To store count of subsequence
Count = {}
# Traverse the array
for i in range(N):
# Store count of subsequence
Count[arr[i]] = pow(2, N - i - 1)
# Print the count of subsequence
for it in Count.values():
print(it, end = " ")
# Driver code
if __name__ == "__main__":
arr = [ 5, 2, 1 ]
N = len(arr)
# Function call
solve(arr, N)
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function that count the subsequence
// such that each element as the
// minimum element in the subsequence
static void solve(int []arr, int N)
{
Dictionary M = new Dictionary();
// Store index in a map
for (int i = 0; i < N; i++)
{
M.Add(i, arr[i]);
}
// Sort the array
Array.Sort(arr);
// To store count of subsequence
Dictionary Count = new Dictionary();
// Traverse the array
for (int i = 0; i < N; i++)
{
// Store count of subsequence
Count.Add(arr[i],
(int)Math.Pow(2, N - i - 1));
}
// Print the count of subsequence
foreach(KeyValuePair m in M)
{
Console.Write(Count[m.Value]);
}
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 5, 2, 1 };
int N = arr.Length;
// Function call
solve(arr, N);
}
}
// This code is contributed by PrinciRaj1992
4 2 1
时间复杂度: O(N * log N)
辅助空间: O(N)