Russian Doll Envelopes
LeetCode 354 · कठिन · समाधान, intuition और कदम-दर-कदम वॉकथ्रू
Sort (w ↑, h ↓), then LIS on heights.
मैंने 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
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.
मैंने कैसे हल किया · पहले विचार से अंतिम हल तक
lru_cache dfs(index, prev_index).
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.