Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb2d19cb

Browse files
add 354
1 parentfc898aa commitb2d19cb

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ Your ideas/fixes/algorithms are more than welcome!
243243
|357|[Count Numbers with Unique Digits](https://leetcode.com/problems/count-numbers-with-unique-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_357.java)| O(?)|O(?)| Medium|
244244
|356|[Line Reflection](https://leetcode.com/problems/line-reflection/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_356.java)| O(n)|O(n) | Medium| HashSet
245245
|355|[Design Twitter](https://leetcode.com/problems/design-twitter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_355.java)| O(n)|O(n) | Medium| Design, HashMap, Heap
246+
|354|[Russian Doll Envelopes](https://leetcode.com/problems/russian-doll-envelopes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_354.java)| O(nlogn)|O(1) | Hard| DP, Binary Search
246247
|353|[Design Snake Game](https://leetcode.com/problems/design-snake-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_353.java)| O(?)|O(?)| Medium|
247248
|352|[Data Stream as Disjoint Intervals](https://leetcode.com/problems/data-stream-as-disjoint-intervals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_352.java)| O(logn)|O(n) | Hard| TreeMap
248249
|351|[Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_351.java)| O(?)|O(?)| Medium|
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Arrays;
4+
5+
/**
6+
* 354. Russian Doll Envelopes
7+
*
8+
* You have a number of envelopes with widths and heights given as a pair of integers (w, h).
9+
* One envelope can fit into another if and only if both the width and height of one envelope is greater than
10+
* the width and height of the other envelope.
11+
12+
What is the maximum number of envelopes can you Russian doll? (put one inside other)
13+
14+
Example:
15+
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]).
16+
17+
*/
18+
publicclass_354 {
19+
publicintmaxEnvelopes(int[][]envelopes) {
20+
if (envelopes ==null ||envelopes.length ==0
21+
||envelopes[0].length ==0 ||envelopes[0].length !=2) {
22+
return0;
23+
}
24+
Arrays.sort(envelopes, (int[]a,int[]b) ->
25+
{
26+
if (a[0] ==b[0])returnb[1] -a[1];
27+
elsereturna[0] -b[0];
28+
}
29+
);
30+
int[]dp =newint[envelopes.length];
31+
intlen =0;
32+
for (int[]envelope :envelopes) {
33+
intindex =Arrays.binarySearch(dp,0,len,envelope[1]);
34+
if (index <0) {
35+
index = -(index+1);
36+
}
37+
dp[index] =envelope[1];
38+
if (index ==len) {
39+
len++;
40+
}
41+
}
42+
returnlen;
43+
}
44+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp