概述
原题网址: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;
}
}
最后
以上就是顺心小松鼠为你收集整理的LeetCode 354. Russian Doll Envelopes(信封包装)的全部内容,希望文章能够帮你解决LeetCode 354. Russian Doll Envelopes(信封包装)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复