Advertisement

Russian Doll Envelopes - LeetCode 354 Solution

Russian Doll Envelopes - Complete Solution Guide

Russian Doll Envelopes is LeetCode problem 354, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given a 2D array of integers envelopes where envelopes[i] = [w i , h i ] 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 maxim

Detailed Explanation

The problem asks us to find the maximum number of Russian doll envelopes, where one envelope can fit inside another if and only if both its width and height are strictly greater than the inner envelope's width and height. We are given a 2D array 'envelopes' where each element 'envelopes[i]' is a pair '[width_i, height_i]' representing the dimensions of an envelope. We cannot rotate envelopes. The goal is to return the maximum number of envelopes that can be nested within each other.

Solution Approach

The solution first sorts the envelopes based on width and height as described in the key insights. Then, it iterates through the sorted envelopes and extracts the height of each envelope. For each height, it performs a binary search in the 'sub' array (which initially is empty). The 'sub' array stores the smallest ending element of each increasing subsequence length found so far. If the height is greater than all elements in 'sub', it extends the LIS by adding the height to 'sub'. Otherwise, it replaces the smallest element in 'sub' that is greater than or equal to the current height with the current height. This substitution maintains the property that 'sub' contains the smallest ending elements, helping to build the LIS efficiently. Finally, the length of the 'sub' array is returned, representing the length of the LIS, which is the maximum number of nested envelopes.

Step-by-Step Algorithm

  1. Step 1: Sort the 'envelopes' array based on width in ascending order and height in descending order (for equal widths).
  2. Step 2: Initialize an empty array 'sub' to store the smallest tail elements of increasing subsequences.
  3. Step 3: Iterate through the sorted 'envelopes', extracting the height of each envelope.
  4. Step 4: Perform a binary search on 'sub' to find the insertion point for the current height.
  5. Step 5: If the insertion point is the end of 'sub', append the height to 'sub' (extend the longest increasing subsequence).
  6. Step 6: Otherwise, replace the element at the insertion point with the current height (potentially find a smaller tail for a subsequence of the same length).
  7. Step 7: After iterating through all envelopes, return the length of 'sub', which represents the maximum number of nested envelopes.

Key Insights

  • Insight 1: Sorting the envelopes is crucial. Sort by width in ascending order. When widths are the same, sort by height in descending order. This ensures that envelopes with equal widths cannot be nested inside each other.
  • Insight 2: After sorting, the problem transforms into finding the Longest Increasing Subsequence (LIS) of the heights. The descending sort on heights for same widths is essential to avoid incorrectly including envelopes with the same width in the LIS.
  • Insight 3: Using binary search (or equivalent) optimizes finding the correct position to update the 'sub' array, which stores the smallest end elements of the LIS of different lengths encountered so far. This ensures an O(n log n) time complexity.

Complexity Analysis

Time Complexity: O(nlogn)

Space Complexity: O(n)

Topics

This problem involves: Array, Binary Search, Dynamic Programming, Sorting.

Companies

Asked at: Atlassian, Intuit, Sprinklr.