Tensorum
YouTubeশেখা শুরু করুন
LeetCode 354 · LIS

Russian Doll Envelopes

LeetCode 354 · কঠিন · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

Sort (w ↑, h ↓), then LIS on heights.

কঠিনArrayBinary SearchDynamic ProgrammingSortingLongest Increasing Subsequence
LeetCode-এ খুলুন ↗

আমি Russian Doll Envelopes (LeetCode 354) এভাবে সমাধান করেছি — LIS প্যাটার্নের একটি কঠিন প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।

নিচের বিবরণ ইংরেজিতে।

Intuition

Sort by width so any nesting chain moves left to right; then you only need heights to strictly increase — an LIS. The trick: for equal widths, sort heights descending so two envelopes with the same width can never both be picked by the LIS.

Approach

  1. 1

    First attempt: memo on (index, previous index) — correct but O(n²) states, too slow for large inputs.

  2. 2

    Sort envelopes by width ascending and height descending.

  3. 3

    Extract the heights and run the O(n log n) patience LIS.

  4. 4

    The LIS length is the number of envelopes you can nest.

আমি কীভাবে সমাধান করেছি · প্রথম ভাবনা থেকে চূড়ান্ত সমাধান

1
Pick / skip + memoO(n²) time · O(n²) space

lru_cache dfs(index, prev_index).

2
Sort + LIS (bisect)AcceptedO(n log n) time · O(n) space

Sort trick turns 2-D into 1-D LIS.

Pick / skip + memoPythonO(n²) time · O(n²) space
from functools import lru_cache 
class Solution:
    def maxEnvelopes(self, envelopes: List[List[int]]) -> int:
        envelopes.sort()
        @lru_cache(maxsize=None)
        def dfs(index,prev_index):
            if index==len(envelopes):
                return 0
            pick = 0
            if prev_index==-1 or (envelopes[index][0]>envelopes[prev_index][0] and envelopes[index][1]>envelopes[prev_index][1]):
                pick=1+dfs(index+1,index)
            dont_pick=dfs(index+1,prev_index)
            return max(pick,dont_pick)
        return dfs(0,-1)
Sort + LIS (bisect)PythonO(n log n) time · O(n) space
from bisect import bisect_left
class Solution:
    def maxEnvelopes(self, envelopes: List[List[int]]) -> int:
       #sort after that build LIS 
       envelopes.sort(key=lambda x:(x[0],-x[1]))
       def lis(nums):
            subs=[nums[0]]
            for num in nums:
                if num>subs[-1]:
                    subs.append(num)
                else:
                    k = bisect_left(subs,num)
                    subs[k]=num
            return len(subs)

       return lis([i[1] for i in envelopes])
স্টেপ-থ্রু লোড হচ্ছে…

Russian Doll Envelopes — সাধারণ প্রশ্ন

Russian Doll Envelopes-এর পেছনে intuition কী?

Sort by width so any nesting chain moves left to right; then you only need heights to strictly increase — an LIS. The trick: for equal widths, sort heights descending so two envelopes with the same width can never both be picked by the LIS.

Russian Doll Envelopes (LeetCode 354) কীভাবে সমাধান করবেন?

1. First attempt: memo on (index, previous index) — correct but O(n²) states, too slow for large inputs. 2. Sort envelopes by width ascending and height descending. 3. Extract the heights and run the O(n log n) patience LIS. 4. The LIS length is the number of envelopes you can nest.

Russian Doll Envelopes-এর time আর space complexity কত?

Time: O(n log n)। Space: O(n)। (Accepted approach: Sort + LIS (bisect)।)

Russian Doll Envelopes-এ সাধারণ ভুলগুলো কী?

Without height-descending on ties, [3,4] and [3,5] would wrongly nest. Both width and height must be strictly larger.

Russian Doll Envelopes সমাধানের কি একাধিক উপায় আছে?

Pick / skip + memo (O(n²) time · O(n²) space): lru_cache dfs(index, prev_index). Sort + LIS (bisect) (O(n log n) time · O(n) space): Sort trick turns 2-D into 1-D LIS.