Leetcode - Russian Doll Envelopes
My code:
public class Solution {
public int maxEnvelopes(int[][] envelopes) {
if (envelopes == null || envelopes.length == 0 || envelopes[0].length == 0) {
return 0;
}
Arrays.sort(envelopes, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
if (a[0] != b[0]) {
return a[0] - b[0];
}
else {
return b[1] - a[1];
}
}
});
List<Integer> rights = new ArrayList<Integer>();
for (int i = 0; i < envelopes.length; i++) {
if (rights.size() == 0 || rights.get(rights.size() - 1) < envelopes[i][1]) {
rights.add(envelopes[i][1]);
}
else {
int begin = 0;
int end = rights.size() - 1;
while (begin <= end) {
int mid = begin + (end - begin) / 2;
if (rights.get(mid) < envelopes[i][1]) {
begin = mid + 1;
}
else if (rights.get(mid) > envelopes[i][1]) {
end = mid - 1;
}
else {
begin = mid;
break;
}
}
rights.set(begin, envelopes[i][1]);
}
}
return rights.size();
}
}
reference:
http://www.programcreek.com/2016/08/leetcode-russian-doll-envelopes-java/
這道題目沒能做出來。看了解法,還是很精彩的。
先按照width大小排序,升序。
再按照height大小排序,降序。
我們可以保證,下一個envelope,他的width一定是當前最大。
但是他的height,並不確定,可能很小,可能很大。
我們需要不斷更新這個右側。
如圖:
具體自己理解吧。
Anyway, Good luck, Richardo! -- 10/12/2016
相關文章
- LeetCode-Russian Doll EnvelopesLeetCode
- OGEM Bentonite mud treatment system for Russian clientclient
- OGEM Solids Control HCQ Vacuum degasser to RussianSolid
- 【LeetCode】如何學習LeetCode?LeetCode
- 2024-2025 ICPC, NERC, Southern and Volga Russian Regional Contest 個人題解(A,B,C,G,J,K,L,N)
- LeetcodeLeetCode
- LeetCode in actionLeetCode
- Leetcode AnagramsLeetCode
- Leetcode SortListLeetCode
- Leetcode ReorderListLeetCode
- leetcode SubsetsLeetCode
- Leetcode PermutationsLeetCode
- [LeetCode刷題筆記] 關於LeetCode的前言LeetCode筆記
- 我的 LeetCodeLeetCode
- Leetcode學習LeetCode
- LeetCode 53 JSLeetCodeJS
- LeetCode 50 JSLeetCodeJS
- LeetCode 克隆圖LeetCode
- Leetcode Perfect SquaresLeetCode
- Leetcode Sort ColorsLeetCode
- Leetcode Sort ArrayLeetCode
- 【LeetCode】Jewels and StonesLeetCode
- 嘗試 LeetcodeLeetCode
- LeetCode 消除遊戲LeetCode遊戲
- Leetcode03LeetCode
- Leetcode LRU CacheLeetCode
- Leetcode Path SumLeetCode
- LeetCode Min StackLeetCode
- Leetcode Word SearchLeetCode
- Leetcode Number of islandsLeetCode
- Leetcode-394LeetCode
- Leetcode 921 JavascriptLeetCodeJavaScript
- golang刷leetcodeGolangLeetCode
- LeetCode:快速排序LeetCode排序
- [LeetCode] Group AnagramLeetCode
- leetcode690LeetCode
- LeetCode 283 jsLeetCodeJS
- Leetcode #463 JavascriptLeetCodeJavaScript