Tensorum
YouTubeशिकायला सुरुवात करा
LeetCode 354 · LIS

Russian Doll Envelopes

LeetCode 354 · कठीण · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू

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

कठीणArrayBinary SearchDynamic ProgrammingSortingLongest Increasing Subsequence

मी 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.