354. Russian Doll Envelopes

本文探讨了一道经典的编程问题——俄罗斯套娃信封问题,通过详细的代码示例介绍了如何利用排序与动态规划来解决该问题,并进一步优化到NlogN的时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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求LIS类似的思路,复杂度O(N^2),


import java.util.Arrays;
import java.util.Comparator;

public class Solution {
    public int maxEnvelopes(int[][] envelopes) {
        if(envelopes == null || envelopes.length == 0)  return 0;
        Arrays.sort(envelopes, new Comparator<int[]>(){
			public int compare(int[] o1, int[] o2) {
				if(o1[0] == o2[0])	return o1[1]-o2[1];
				return o1[0]-o2[0];
			}
        });
        
        // dp[i] means best result from envelopes[0] to envelopes[i](necessary included) 
        int[] dp = new int[envelopes.length];
        int[] curMax = new int[dp.length];
        Arrays.fill(dp, 1);
        Arrays.fill(curMax, 1);
        int max = 1;
        for(int i=1; i<dp.length; i++) {
        	for(int j=i-1; j>=0; j--)
        		if(envelopes[j][0]<envelopes[i][0] && envelopes[j][1]<envelopes[i][1]) {
        			dp[i] = Math.max(dp[i], dp[j]+1);
        			if(dp[i] == curMax[i])	break;
        		}
        	max = Math.max(max, dp[i]);
        	curMax[i] = Math.max(curMax[i-1], dp[i]);
        }
        
        return max;
    }
}


后来发现求LIS可以有N*logN的解法,如下:


那这个题能不能用呢?如果按照上面的排序策略是不行的,因为你不能确定(2,5),(3,4)谁比较小,这取决于后面的数据,

换种思路:已经按照width排好了序,那我们只需要考虑height,求height的LIS,但是相同的width的只能有一个啊,那相同的width就按照height降序排,这样一旦选取了某个height,后面相同width的height就不可能选了,前面的也不可以,in this way,NlogN is achieved

import java.util.Arrays;
import java.util.Comparator;
/*
 * 1. Sort: Ascend on width, descent on height
 * 2. find the longest increasing subsequence based on height
 * 
 * in this way, the same width can only have one and we can run LIS using binary search.
 * since with same width, latter coming will always has smaller height, hi will always decrease until 
 * cross over the reigon of same width
 */
public class Solution {
    public int maxEnvelopes(int[][] envelopes) {
        if(envelopes == null || envelopes.length == 0)  return 0;
        Arrays.sort(envelopes, new Comparator<int[]>(){
			public int compare(int[] o1, int[] o2) {
				if(o1[0] == o2[0])	return o2[1]-o1[1];
				return o1[0]-o2[0];
			}
        });
        
        // LIS, since the height, we can do it in NlogN 
        int[] h = new int[envelopes.length];
        int len = 0;
        
        for(int[] a : envelopes) {
        	int insert = Arrays.binarySearch(h, 0, len, a[1]);
        	if(insert < 0)	insert = -(insert+1);	// if not find
        	h[insert] = a[1];
        	if(insert == len)	len++;
        }
        
        return len;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值