WEEK#5 Dynamic Programming_Maximum Length of Pair Chain

Description of the Problem

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].


A rough idea

1.Sort the pairs by their smaller number
2.Select the first pair, who has the smallest first number, and set smallest = 0
3.Traverse all pairs by i

  1. if SmallestPair[smallest][1] < CurrentPair[i][0]
  2. result++; smallest = i;
    bool SortFunction(vector<int> v1, vector<int> v2) {
        return v1[0] < v2[0];
    }

class Solution {
public:
    int findLongestChain(vector<vector<int>>& pairs) {
        sort(pairs.begin(), pairs.end(), SortFunction);
        int result = 1;
        int Min = pairs[0][0]; // minimun number in all pairs
        int MinIndex = 0; // index of pairs in which minimun number exists
        for (int i = 0 ; i < pairs.size(); i++) {
            if (pairs[MinIndex][1] < pairs[i][0]) {
                Min = pairs[i][0];
                MinIndex = i;
                result++;
            }
        }
        return result;
    }
};

Such program gets a wrong answer :

WA.png

Analysis:

Having neglected the situations where the first number be the smallest of all and the second number be the largest of all. In this way, the length of the chain would be only 1.

Correction

Sort by the larger number. Because the smaller the larger number is, the bigger the range in which we can select.

    bool SortFunction(vector<int> v1, vector<int> v2) {
        return v1[1] < v2[1];
    }

class Solution {
public:
    int findLongestChain(vector<vector<int>>& pairs) {
        sort(pairs.begin(), pairs.end(), SortFunction);
        int result = 1;
        int Min = pairs[0][0]; // minimun number in all pairs
        int MinIndex = 0; // index of pairs in which minimun number exists
        for (int i = 0 ; i < pairs.size(); i++) {
            if (pairs[MinIndex][1] < pairs[i][0]) {
                Min = pairs[i][0];
                MinIndex = i;
                result++;
            }
        }
        return result;
    }
};
AC.png

Not exactly an ideal solution though. Should look for improvement.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 职场,对于一个新人来说,有些规则是你必须要了解的。一旦你不小心触碰到了这些底线,好的情况可能就是被批一顿,最糟糕的...
    无言语焉阅读 4,511评论 3 13
  • 民以食为天,前面曾说到如何科学的选择餐点,给出了相关建议。可也有人问道,我们生活有些人可不都是出去吃的,点外卖反而...
    思践于人阅读 3,321评论 0 4
  • 这大概已经是几周前的事了吧,M坐在我的对面,我静静的看着她的脸,跟同龄人比起来略显沧桑,脸上都是岁月留下的痕迹,有...
    周小妖阅读 5,614评论 3 5

友情链接更多精彩内容