📜  对的最大长度链|套装2

📅  最后修改于: 2021-04-21 23:16:31             🧑  作者: Mango

给定一个大小为N的数字对的数组。在每对中,第一个数字始终小于第二个数字。如果b

例子:

方法:这里讨论了针对该问题的动态编程方法。
想法是使用贪婪方法解决问题,这与活动选择问题相同。

  • 按照每对第二个数字的升序对所有对进行排序。
  • 选择first no作为第一对链,并使用第一对链的第二个值设置变量s(say)。
  • 从数组的第二对迭代到最后对,如果当前对的第一个元素的值较大,则先前选择的对将选择当前对,并更新最大长度和变量s的值。
  • 返回最大链长的值。

下面是上述方法的实现。

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Structure for storing pairs
// of first and second values.
struct val {
    int first;
    int second;
};
 
// Comparator function which can compare
// the second element of structure used to
// sort pairs in increasing order of second value.
bool comparator(struct val p1, struct val p2)
{
    return (p1.second < p2.second);
}
 
// Function for finding max length chain
int maxChainLen(struct val p[], int n)
{
    // Initialize length l = 1
    int l = 1;
 
    // Sort all pair in increasing order
    // according to second no of pair
    sort(p, p + n, comparator);
 
    // Pick up the first pair and assign the
    // value of second element fo pair to a
    // temporary variable s
    int s = p[0].second;
 
    // Iterate from second pair (index of
    // the second pair is 1) to the last pair
    for (int i = 1; i < n; i++) {
 
        // If first of current pair is greater
        // than previously selected pair then
        // select current pair and update
        // value of l and s
        if (p[i].first > s) {
            l++;
            s = p[i].second;
        }
    }
 
    // Return maximum length
    return l;
}
 
// Driver Code
int main()
{
 
    // Declaration of array of structure
    val p[] = { { 5, 24 }, { 39, 60 },
              { 15, 28 }, { 27, 40 }, { 50, 90 } };
 
    int n = sizeof(p) / sizeof(p[0]);
 
    // Fucntion call
    cout << maxChainLen(p, n) << endl;
 
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
 
// Structure for storing pairs
// of first and second values.
class GFG{
     
// Class for storing pairs
// of first and second values.
static class Pair
{
    int first;
    int second;
     
    Pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
};
     
// Function for finding max length chain
static int maxChainLen(Pair p[], int n)
{
     
    // Initialize length l = 1
    int l = 1;
 
    // Sort all pair in increasing order
    // according to second no of pair
    Arrays.sort(p, new Comparator()
    {
        public int compare(Pair a, Pair b)
        {
            return a.second - b.second;
        }
    });
 
    // Pick up the first pair and assign the
    // value of second element fo pair to a
    // temporary variable s
    int s = p[0].second;
 
    // Iterate from second pair (index of
    // the second pair is 1) to the last pair
    for(int i = 1; i < n; i++)
    {
         
        // If first of current pair is greater
        // than previously selected pair then
        // select current pair and update
        // value of l and s
        if (p[i].first > s)
        {
            l++;
            s = p[i].second;
        }
    }
     
    // Return maximum length
    return l;
}
 
// Driver Code
public static void main(String args[])
{
     
    // Declaration of array of structure
    Pair p[] = new Pair[5];
 
    p[0] = new Pair(5, 24);
    p[1] = new Pair(39, 60);
    p[2] = new Pair(15, 28);
    p[3] = new Pair(27, 40);
    p[4] = new Pair(50, 90);
     
    int n = p.length;
     
    // Fucntion call
    System.out.println(maxChainLen(p, n));
}
}
 
// This code is contributed by adityapande88


输出:
3

时间复杂度: O(N * log(N))