给定一组大小为N的数字对。在每一对中,第一个数字总是小于第二个数字。如果 b < c,一对 (c, d) 可以跟在另一对 (a, b) 之后。可以以这种方式形成对链。任务是找到可以由给定的一组对形成的最长链的长度。
例子:
Input: N = 5, arr={{5, 24}, {39, 60}, {15, 28}, {27, 40}, {50, 90} }
Output: 3
The longest chain that can be formed is of length 3, and the chain is {{5, 24}, {27, 40}, {50, 90}}.
Input : N = 2, arr={{5, 10}, {1, 11}}
Output :1
方法:此处讨论了针对该问题的动态规划方法。
想法是使用贪心方法解决问题,这与活动选择问题相同。
- 按每对的第二个数字的升序对所有对进行排序。
- 首先选择 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]);
// Function 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;
// Function call
System.out.println(maxChainLen(p, n));
}
}
// This code is contributed by adityapande88
输出:
3
时间复杂度: O(N*log(N))
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。