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.