lc 646 Maximum Length of Pair Chain


646 Maximum Length of Pair Chain

You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.

Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion.

Given a set of pairs, find the length longest chain which can be formed. You needn't use up all the given pairs. You can select pairs in any order.

Example 1:

Input: [[1,2], [2,3], [3,4]]
Output: 2
Explanation: The longest chain is [1,2] -> [3,4]

Note:

The number of given pairs will be in the range [1, 1000].

DP Accepted

dp[i]表示前i+1个可以组成链的数对个数,先将数对根据第一个数进行从小到大的排序,可以得出动态转移方程为dp[i] = max(dp[i], pairs[i][0] > pairs[j][1]? dp[j] + 1 : dp[j])

class Solution {
public:
int findLongestChain(vector<vector<int>>& pairs) {
sort(pairs.begin(), pairs.end(), cmp);
vector<int> dp(pairs.size(), 1);
for (int i = 0; i < pairs.size(); i++) {
for (int j = 0; j < i; j++) {
dp[i] = max(dp[i], pairs[i][0] > pairs[j][1]? dp[j] + 1 : dp[j]);
}
}
return dp[pairs.size()-1];
} static bool cmp(vector<int>& a, vector<int>& b) {
return a[0] < b[0];
}
};

普遍方法 Accepted

根据每一对的第二个值将所有数对进行从小到大的排序,此时,循环遍历整个数组如果前一成链数对的第二个数小于当前遍历数对的第一个数,则又可以成链。这个方法很直观且简单。

class Solution {
public:
int findLongestChain(vector<vector<int>>& pairs) {
sort(pairs.begin(), pairs.end(), cmp);
int maxlen = 1;
for (int i = 0, j = 1; j < pairs.size(); j++) {
if (pairs[i][1] < pairs[j][0]) {
maxlen++;
i = j;
}
}
return maxlen;
} static bool cmp(vector<int>& a, vector<int>& b) {
return a[1] < b[1];
}
};

LN : leetcode 646 Maximum Length of Pair Chain的更多相关文章

  1. LC 646. Maximum Length of Pair Chain

    You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...

  2. 【LeetCode】646. Maximum Length of Pair Chain 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...

  3. 646. Maximum Length of Pair Chain(medium)

    You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...

  4. 646. Maximum Length of Pair Chain 最长的链条长度

    [抄题]: You are given n pairs of numbers. In every pair, the first number is always smaller than the s ...

  5. 646. Maximum Length of Pair Chain

    You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...

  6. LeetCode Maximum Length of Pair Chain

    原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n  ...

  7. [LeetCode] Maximum Length of Pair Chain 链对的最大长度

    You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...

  8. [Swift]LeetCode646. 最长数对链 | Maximum Length of Pair Chain

    You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...

  9. [LeetCode] 718. Maximum Length of Repeated Subarray 最长的重复子数组

    Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...

随机推荐

  1. sqlserver中All、Any和Some用法与区别

    转自:http://blog.csdn.net/gyc1105/article/details/8063624 SQLServer中有三个关键字可以修改比较运算符:All.Any和Some,其中Som ...

  2. 移动端html5页面长按实现高亮全选文本内容的兼容解决方式

    近期须要给html5的WebAPP在页面上实现一个复制功能:用户点击长按文本会全选文字并弹出系统"复制"菜单.用户能够点击"复制"进行复制操作.然后粘贴到App ...

  3. [转] logback logback.xml常用配置详解(一)<configuration> and <logger>

    转载文章:原文出处:http://aub.iteye.com/blog/1101260 详细整理了logback常用配置, 不是官网手册的翻译版,而是使用总结,旨在更快更透彻的理解其配置 根节点< ...

  4. HOSVD高阶奇异值分解

    高阶奇异值分解(High Order Singular Value  Decomposition,   HOSVD) 奇异值分解SVD(Singular Value Decomposition)是线性 ...

  5. envoy

    微服务意味着网络更加依赖于服务抽象边界. 随着相互依赖的服务数量日渐增长,系统100%没问题的时间会变少,整个系统经常有部分功能处于降级状态.

  6. h5ai目录列表优化

    h5ai是HTTP Web服务器的现代文件索引器,专注于您的文件.目录以有吸引力的方式显示,浏览它们通过不同的视图,面包屑和树状概述增强.最初,h5ai是HTML5 Apache Index的缩写,但 ...

  7. Fri Jul 28 16:28:52 CST 2017 WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection mus

    Fri Jul 28 16:28:52 CST 2017 WARN: Establishing SSL connection without server’s identity verificatio ...

  8. Lightoj 1166 - Old Sorting

    Given an array containing a permutation of 1 to n, you have to find the minimum number of swaps to s ...

  9. The android gradle plugin version 2.3.0-beta2 is too old, please update to the latest version.

    编译项目的时候,报如下错误: Error:(, ) A problem occurred evaluating project ':app'. > Failed to apply plugin ...

  10. Count on an IEnumerable<dynamic>

    http://stackoverflow.com/questions/7733305/count-on-an-ienumerabledynamic int count = Enumerable.Cou ...