leetcode 354. Russian Doll Envelopes 俄罗斯套娃 + 动态规划DP + 类似LISS最长递增子序列的做法

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 the maximum number of envelopes can you Russian doll? (put one inside other)

Example:
Given envelopes = [[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).

这道题就是类似俄罗斯套娃,只需要把信封做一次排序,然后动态规划即可。

这道题的启发就是说要学会使用DP动态规划解决问题。

建议和leetcode 300. Longest Increasing Subsequence 最长递增子序列LISS + 十分经典的动态规划DP做法leetcode 368. Largest Divisible Subset 类似LISS最长递增子序列问题 + DP动态规划leetcode 646. Maximum Length of Pair Chain 最长连续元素 一起学习

代码如下:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

class Point
{
    int width;
    int height;
    public Point(int a,int b) 
    {
        width=a;
        height=b;
    }
}

/*
 * 要习惯的使用DP去解决问题
 * dp[i]表示已i个信封结束最多有n个嵌套
 * 然后循环更新dp[i]即可
 * 这个问题挺简单的
 * */
public class Solution 
{
    public int maxEnvelopes(int[][] envelopes) 
    {
        if(envelopes==null || envelopes.length<=0)
            return 0;

        List<Point> list=new ArrayList<Point>();
        for(int i=0;i<envelopes.length;i++)
        {
            Point one=new Point(envelopes[i][0], envelopes[i][1]);
            list.add(one);
        }
        list.sort(new Comparator<Point>() {
            @Override
            public int compare(Point a, Point b) {
                if(a.width!=b.width)
                    return a.width-b.width;
                else 
                    return a.height-b.height;
            }
        });

        int []dp=new int[list.size()];
        Arrays.fill(dp, 1);
        for(int i=1;i<list.size();i++)
        {
            Point fa=list.get(i);
            for(int j=0;j<i;j++)
            {
                Point now=list.get(j);
                if(fa.height>now.height && fa.width>now.width)
                    dp[i]=Math.max(dp[i], dp[j]+1);
            }
        }

        int max=dp[0];
        for(int i=1;i<dp.length;i++)
            max=Math.max(max, dp[i]);

        return max;
    }
}

下面是C++的做法

仔细的想一下,本题就是LISS最长递增子序列的一个变形,所以要学会举一反三的思考和解决问题

代码如下:

#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <sstream>
#include <functional>
#include <bitset>
#include <numeric>
#include <cmath>
#include <regex>

using namespace std;



bool cmp(pair<int, int> a, pair<int, int> b)
{
    if (a.first != b.first)
        return a.first <= b.first;
    else
        return a.second <= b.second;
}

class Solution 
{
public:
    int maxEnvelopes(vector<pair<int, int>>& envelopes) 
    {
        vector<pair<int, int>> all = envelopes;
        sort(all.begin(), all.end(), cmp);

        int res = 0;
        vector<int> dp(all.size(), 1);
        for (int i = 0; i < all.size(); i++)
        {
            for (int j = 0; j < i; j++)
            {
                if (all[i].first > all[j].first && all[i].second > all[j].second && dp[j] + 1 > dp[i])
                    dp[i] = dp[j] + 1;
            }
            res = max(res, dp[i]);
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值