给定大小为n的数组A [] ,其中数组中只有唯一元素。我们必须找到序列A所需的最小串联,才能获得严格的最长递增子序列。对于数组A [],我们遵循基于1的索引。
例子:
Input: A = {1, 3, 2}
Output: 2
Explanation:
We can concatenate A two times as [1, 3, 2, 1, 3, 2] and then output for index 1, 3, 5 which gives sub-sequence as 1->2->3.
Input: A = {2, 1, 4, 3, 5}
Output: 3
Explanation:
The given array has to be concatenated 3 times to generate the Longest Increasing Subsequence.
方法:
为了解决上述问题,第一个观察结果是严格增加的子序列的长度始终等于序列A []中存在的唯一元素的数量。因此,我们必须找出一种方法来保持最大连续数字顺序在一起。我们可以通过在选择串联之前在序列中获取尽可能多的严格连续的数字并在下一个串联中处理其他数字来实现此目的。
- 形成元素对及其索引,并按值按排序顺序存储它们。初始化变量以计算所需的串联操作。
- 运行从索引2到n的循环,如果对[i-1]的对>对[i]的索引,则将变量加1,该计数正在计算所需的串联操作,否则继续该过程。
下面是上述方法的实现:
C++
// CPP implementation to Find the minimum
// concatenation required to get strictly
// Longest Increasing Subsequence for the
// given array
#include
using namespace std;
int increasingSubseq(int a[], int n)
{
// Ordered map containing pairs
// of value and index.
map mp;
for (int i = 0; i < n; i++) {
// Form pairs of value at index i
// and it's index that is i.
mp.insert(make_pair(a[i], i));
}
// Variable to insert the minimum
// concatenations that are needed
int ans = 1;
// Iterator pointing to the
// second pair in the map
auto itr = ++mp.begin();
// Iterator pointing to the
// first pair in the map
for (auto it = mp.begin(); it != mp.end(); ++it) {
// Check if itr tends to end of map
if (itr != mp.end()) {
// Check if index2 is not greater than index1
// then we have to perform a concatenation.
if (itr->second <= it->second)
ans += 1;
++itr;
}
}
// Return the final answer
cout << ans << endl;
}
// Driver code
int main()
{
int a[] = { 2, 1, 4, 3, 5 };
int n = sizeof(a) / sizeof(a[0]);
increasingSubseq(a, n);
return 0;
}
Java
/*package whatever //do not write package name here */
import java.util.*;
import java.io.*;
class GFG {
private static void increasingSubseq(int a[], int n)
{
// Ordered map containing pairs
// of value and index.
TreeMap mp
= new TreeMap();
for (int i = 0; i < n; i++) {
// Form pairs of value at index i
// and it's index that is i.
mp.put(a[i], i);
}
// Variable to insert the minimum
// concatenations that are needed
int ans = 1;
// Iterator pointing to the
// first entry in the map
Map.Entry itr
= mp.pollFirstEntry();
for (Map.Entry it :
mp.entrySet())
{
// Check if index2 is not greater than index1
// then we have to perform a concatenation.
if (itr.getValue() >= it.getValue())
ans++;
itr = it;
}
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
int a[] = { 2, 1, 4, 3, 5 };
int n = a.length;
increasingSubseq(a, n);
}
}
Python3
# Python 3 implementation to
# Find the minimum concatenation
# required to get strictly Longest
# Increasing Subsequence for the
# given array
def increasingSubseq(a, n):
# Ordered map containing pairs
# of value and index.
mp = {}
for i in range(n):
# Form pairs of value at
# index i and it's index
# that is i.
mp[a[i]] = i
# Variable to insert the
# minimum concatenations
# that are needed
ans = 1
# Iterator pointing to the
# second pair in the map
itr = 1
li= list(mp.values())
# Iterator pointing to the
# first pair in the map
for key, value in mp.items():
# Check if itr tends to
# end of map
if (itr < len(mp)):
# Check if index2 is not
# greater than index1
# then we have to perform
# a concatenation.
if (li[itr] <= key):
ans += 1
itr+=1
# Return the final
# answer
print(ans)
# Driver code
if __name__ == '__main__':
a = [2, 1, 4, 3, 5]
n = len(a)
increasingSubseq(a, n)
# This code is contributed by bgangwar59
输出
3
时间复杂度: O(N * log N)