📌  相关文章
📜  获得给定数组的严格 LIS 所需的最小串联

📅  最后修改于: 2021-10-26 06:04:59             🧑  作者: Mango

给定一个大小为 n 的数组 A[] ,其中数组中只有唯一的元素。我们必须找到序列 A 所需的最小串联以获得严格的最长递增子序列。对于数组 A[],我们遵循基于 1 的索引。

例子:

方法:
为了解决上面提到的问题,第一个观察结果是严格递增的子序列的长度总是等于序列 A[] 中存在的唯一元素的数量。因此,我们必须想办法让最大的连续数字保持在一起。我们可以通过在选择串联之前在序列中取尽可能多的严格连续数字来实现这一点,并在下一个串联中处理其他数字。

  • 形成元素对及其索引,并按值按排序顺序存储它们。初始化一个变量以计算所需的串联操作。
  • 运行从索引 2 到 n 的循环,如果 pair[i-1] > pair[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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程