第十二周 Leetcode 354. Russian Doll Envelopes(HARD) LIS问题

Leetcode354 暴力的方法是显而易见的 O(n^2)构造一个DAG找最长链即可。

也有办法优化到O(nlogn)

注意 信封的方向是不能转换的。

对第一维从小到大排序,第一维相同第二维从大到小排序。

维护一个符合题意的队列,当队列中的第二维均比当前信封的第二维小时,必然可以增加到队尾。

如果不然,可以让当前信封作为“替补”,它可以在恰当的时候代替恰好比它大的信封。

当替补们足够替换所有已有信封时,就可以增加新的信封了。

比较抽象,不过这个技巧很有趣。

看代码吧,很清晰。

class Solution {
public:
    static bool cmp_first(const pair<int, int>& i, const pair<int, int>& j) {
        if (i.first == j.first)
            return i.second > j.second;
        return i.first < j.first;
    }
    int maxEnvelopes(vector<pair<int, int>>& envelopes) {
        sort(envelopes.begin(), envelopes.end(), cmp_first);
        vector<int> dp;
        for (int i = 0; i < envelopes.size(); ++i) {
            auto itr = lower_bound(dp.begin(), dp.end(), envelopes[i].second);
            if (itr == dp.end()) {
                dp.push_back(envelopes[i].second);
            } else {
                *itr = envelopes[i].second;
            }
        }
        return dp.size();
  }
};

  

时间: 2024-08-24 12:35:29

第十二周 Leetcode 354. Russian Doll Envelopes(HARD) LIS问题的相关文章

leetCode 354. Russian Doll Envelopes

You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope. What is

[动态规划] leetcode 354 Russian Doll Envelopes

problem:https://leetcode.com/problems/russian-doll-envelopes/ 最长连续子序列类型问题.先排序,dp[i]记录使用第i个套娃的最大数量. bool cmp(const vector<int>& x, const vector<int>& y) { return x[0] == y[0] ? x[1] < y[1] : x[0] < y[0]; } class Solution { public:

【leetcode】354. Russian Doll Envelopes

题目描述: You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope. Wh

[email&#160;protected] [354] Russian Doll Envelopes (Dynamic Programming)

https://leetcode.com/problems/russian-doll-envelopes/ You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater tha

【Leetcode】Russian Doll Envelopes

题目链接:https://leetcode.com/problems/russian-doll-envelopes/ 题目: You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is gr

354. Russian Doll Envelopes

从我花时间上看,我是纠结排序很久.注意一下Comparator的写法,以后会了就好~ 算法上讲,就是对envolope尺寸排序,然后对于排序后的每个信封,它可以装进的最多小信封数,是长宽都比它小的信封中装的进最多数目+1.和之前做的368. Largest Divisible Subset思路是一样的,甚至还要简单一点. 动规. 1 static class MySort implements Comparator<int[]> { 2 public int compare(int[] o1,

第十二周进度条

第十二周          日期  星期一   星期二   星期三   星期四   星期五   星期六   星期日  了解到的知识点 js获取当前时间 var d = new Date() var nowYear = +d.getFullYear() EF框架填充下拉菜单 var model = db.GYSAllFoods.Select(m => new{GYS = m.GYS}).Distinct();//去重很关键            foreach (var item in model

学习进度第十二周

  第十二周 所花时间(包括上课) 11h(4h上课,7课下) 代码量(行) 220 博客量(篇) 1 了解到的知识点 这个星期主要进行了安卓实验和大作业的编写,从中学到了利用安卓SQLite 数据库 进行表的存储以及应用,按照教程成功编写了一个特别小的视频播放器,学会了进度 条等控件的使用.

学习进度-第十二周

  第十二周 所花时间(包括上课) 10小时 代码量(行) 48行 博客量(篇) 3篇 了解到的知识点