Skip to content

Commit ff51591

Browse files
add 354
1 parent 59eac0c commit ff51591

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ LeetCode
227227
|0347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | c | [c++](./src/0347-Top-K-Frequent-Elements/0347.cpp) |[python](./src/0347-Top-K-Frequent-Elements/0347.py)|||Medium|
228228
|0349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | c | [c++](./src/0349-Intersection-of-Two-Arrays/0349.cpp) |[python](./src/0349-Intersection-of-Two-Arrays/0349.py)|||Easy|
229229
|0350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | c | [c++](./src/0350-Intersection-of-Two-Arrays-II/0350.cpp) |[python](./src/0350-Intersection-of-Two-Arrays-II/0350.py)|||Easy|
230+
|0354|[Russian Doll Envelopes](https://leetcode.com/problems/russian-doll-envelopes/) | c | [c++](./src/0354-Russian-Doll-Envelopes/0354.cpp) |[python](./src/0354-Russian-Doll-Envelopes/0354.py)|||Hard|
230231
|0377|[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/) | c | [c++](./src/0377-Combination-Sum-IV/0377.cpp) |[python](./src/0377-Combination-Sum-IV/0377.py)|||Medium|
231232
|0389|[Find the Difference](https://leetcode.com/problems/find-the-difference/) | c | [c++](./src/0389-Find-the-Difference/0389.cpp) |[python](./src/0389-Find-the-Difference/0389.py)|||Easy|
232233
|0392|[Is Subsequence](https://leetcode.com/problems/is-subsequence/) | c | [c++](./src/0392-Is-Subsequence/0392.cpp) |[python](./src/0392-Is-Subsequence/0392.py)|||Medium|

Diff for: src/0354-Russian-Doll-Envelopes/0354.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
2+
class Solution
3+
{
4+
public:
5+
int maxEnvelopes(vector<pair<int, int>>& envelopes)
6+
{
7+
sort(envelopes.begin(), envelopes.end(), [](const pair<int, int>& a, const pair<int, int>& b){
8+
return a.first < b.first || (a.first == b.first and a.second > b.second);
9+
});
10+
11+
vector<int> mem;
12+
for (auto& e : envelopes)
13+
{
14+
auto index = lower_bound(mem.begin(), mem.end(), e.second);
15+
if (index == mem.end()) mem.push_back(e.second);
16+
else *index = e.second;
17+
}
18+
return mem.size();
19+
}
20+
};

Diff for: src/0354-Russian-Doll-Envelopes/0354.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from bisect import bisect_left
2+
class Solution:
3+
def maxEnvelopes(self, envelopes: List[List[int]]) -> int:
4+
if not envelopes:
5+
return 0
6+
7+
res, n = 0, len(envelopes)
8+
envelopes.sort(key=lambda a:(a[0], -a[1]))
9+
mem = list()
10+
for e in envelopes:
11+
index = bisect_left(mem, e[1])
12+
if len(mem) == index:
13+
mem.append(e[1])
14+
else:
15+
mem[index] = e[1]
16+
return len(mem)

0 commit comments

Comments
 (0)