엔지니어 게시판
LeetCode 솔루션 분류

[5/25] 354. Russian Doll Envelopes

컨텐츠 정보

본문

[LeetCode 시즌 3] 2022년 5월 25일 문제입니다.

https://leetcode.com/problems/russian-doll-envelopes/


354. Russian Doll Envelopes
Hard
317377Add to ListShare

You are given a 2D array of integers envelopes where envelopes[i] = [wi, hi] represents the width and the height of an envelope.

One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope's width and height.

Return the maximum number of envelopes you can Russian doll (i.e., put one inside the other).

Note: You cannot rotate an envelope.

 

Example 1:

Input: envelopes = [[5,4],[6,4],[6,7],[2,3]]
Output: 3
Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).

Example 2:

Input: envelopes = [[1,1],[1,1],[1,1]]
Output: 1

 

Constraints:

  • 1 <= envelopes.length <= 105
  • envelopes[i].length == 2
  • 1 <= wi, hi <= 105

관련자료

댓글 1

mingki님의 댓글

  • 익명
  • 작성일
C++
Runtime: 547 ms, faster than 36.76% of C++ online submissions for Russian Doll Envelopes.
Memory Usage: 77.6 MB, less than 70.81% of C++ online submissions for Russian Doll Envelopes.
class Solution {
public:
    int maxEnvelopes(vector<vector<int>>& envelopes) {
        sort(envelopes.begin(), envelopes.end(), [](const vector<int> &a, const vector<int> &b) {
            return a[0] == b[0] ? a[1] > b[1] : a[0] < b[0];
        });
        
        vector<int> dp;
        
        for (auto &e : envelopes) {
            auto low = lower_bound(dp.begin(), dp.end(), e[1]);
            if (low == dp.end()) {
                dp.push_back(e[1]);
            }
            else if (e[1] < *low) {
                *low = e[1];
            }
        }
        return dp.size();
    }
};
전체 410 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 545 명
  • 오늘 방문자 5,744 명
  • 어제 방문자 9,517 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,599 명
알림 0