LeetCode 354. Russian Doll Envelopes(信封包装)

37 篇文章 0 订阅
17 篇文章 0 订阅

原题网址: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 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]).

此题和CareerCup中的马戏团是同一个问题,参见《Cracking the Coding Interview, 6th Edition》第17.8题。

这道题的关键在于能否看出是一个求最长公共子序列的问题。

方法一:使用TreeMap来查找较小的信封,此方法无法通过实现要求(TLE)

public class Solution {
    public int maxEnvelopes(int[][] envelopes) {
        Arrays.sort(envelopes, new Comparator<int[]>() {
            @Override
            public int compare(int[] e1, int[] e2) {
                return e1[0] - e2[0];
            }
        });
        int max = 0;
        int[] counts = new int[envelopes.length];
        TreeMap<Integer, List<Integer>> tm = new TreeMap<>();
        for(int i=0; i<envelopes.length; i++) {
            int[] envelope = envelopes[i];
            Map<Integer, List<Integer>> hm = tm.headMap(envelope[1]);
            for(List<Integer> list: hm.values()) {
                for(int e: list) {
                    if (envelopes[e][0] < envelope[0]) counts[i] = Math.max(counts[i], counts[e]);
                }
            }
            counts[i] ++;
            max = Math.max(max, counts[i]);
            List<Integer> list = tm.get(envelope[1]);
            if (list == null) {
                list = new ArrayList<>();
                tm.put(envelope[1], list);
            }
            list.add(i);
        }
        return max;
    }
}

方法二:先排序再穷举。

public class Solution {
    public int maxEnvelopes(int[][] envelopes) {
        Arrays.sort(envelopes, new Comparator<int[]>() {
            @Override public int compare(int[] e1, int[] e2) {
                return e1[0] - e2[0];
            }
        });
        int max = 0;
        int[] counts = new int[envelopes.length];
        for(int i=0; i<envelopes.length; i++) {
            counts[i] = 1;
            for(int j=0; j<i; j++) {
                if (envelopes[j][0] < envelopes[i][0] && envelopes[j][1] < envelopes[i][1]) {
                    counts[i] = Math.max(counts[i], counts[j] + 1);
                }
            }
            max = Math.max(max, counts[i]);
        }
        return max;
    }
}

方法三:先对宽度进行排序,再应用最长递增子序列的方法,寻找高度递增的最大长度。这个方法太牛了,我没有想到,参考网友的:

https://leetcode.com/discuss/106946/java-nlogn-solution-with-explanation

public class Solution {
    public int maxEnvelopes(int[][] envelopes) {
        Arrays.sort(envelopes, new Comparator<int[]>() {
            @Override
            public int compare(int[] e1, int[] e2) {
                if (e1[0] != e2[0]) return e1[0] - e2[0];
                return e2[1] - e1[1];
            }
        });
        int len = 0;
        int[] h = new int[envelopes.length];
        for(int[] envelope : envelopes) {
            int i=0, j=len-1;
            while (i<=j) {
                int m = (i+j)/2;
                if (h[m] < envelope[1]) i=m+1; else j=m-1;
            }
            h[i] = envelope[1];
            if (i == len) len ++;
        }
        return len;
    }
}

另一种实现:

public class Solution {
    public int maxEnvelopes(int[][] envelopes) {
        if (envelopes.length <= 1) return envelopes.length;
        Arrays.sort(envelopes, new Comparator<int[]>() {
            @Override
            public int compare(int[] e1, int[] e2) {
                int c = e1[0] - e2[0];
                if (c == 0) c = e2[1] - e1[1];
                return c;
            }
        });
        int count = 0;
        for(int i = 0; i < envelopes.length; i++) {
            int left = 0, right = count - 1;
            while (left <= right) {
                int m = left + (right - left) / 2;
                if (envelopes[i][1] > envelopes[m][1]) left = m + 1; else right = m - 1;
            }
            envelopes[left] = envelopes[i];
            if (count == left) count++;
        }
        return count;
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值