# Find the minimum number of rectangles left after inserting one into another

Given **width** and **height** of **N** rectangles. The task is to find the minimum number of rectangles left after inserting one into another.

**Note :**

- If W1 < W2 and H1 < H2 then rectangle 1 fits inside rectangle 2.
- The smallest rectangle can insert in the second smallest, and this rectangle can insert in the next one and so forth.

**Examples:**

Input :arr[] = {{20, 30}, {10, 10}, {30, 20}, {40, 50}};Output :2Explanation :One of the possible way is to insert second recatngle in first and then insert first rectangle in fourth. Then finally, third and fourth rectangles left.Input :arr[] = {{10, 30}, {20, 20}, {30, 10}};Output :3Explanation :Can't place any rectangle in any other one. So, three rectangles left.

**Approach** :

- Firstly sort all rectangles such that the heights are in decreasing order. We will first assume that each height is unique (later we will extend our approach to the case where there are the same heights).
- We maintain another array nested[i]. Starting from the tallest rectangle down to the shortest, we will try to fit in the rectangle to nested[i], by finding a nested rectangle in nested[i] in which its width is bigger than that of our current rectangle. Not only that, we want to place it in the one with the minimum width. We then place the rectangle inside that nested rectangle and update its new height and weight. Why the minimum one? Because if we place the rectangle on another one with a width bigger than that of minimum width, we can apply the following exchange argument:

Let’s assume there exist an optimal arrangement such that the current rectangle[i] is not placed on nested[m] of minimum width that satisfies the above requirement. Suppose rectangle[i] is placed on nested[n], and another rectangle[j] is placed on nested[m] instead. Then since rectangle[j] can fit in nested[n], it can also fit in nested[m], and hence we can swap rectangle[i] and rectangle[j]. By performing the exchange to all such rectangles at all stage, we can transform this optimal arrangement to our greedy arrangement. Hence our greedy arrangement is also optimal. - Inductively, we can prove that the rectangles in nested[i] are always sorted in increasing width.
- Lastly, in the case that there is a rectangle with the same heights, we sort the widths in increasing order so as to maintain the sorting order of nested[i].

Below is the implementation of the above approach:

`// CPP program to find the minimum number of rectangles ` `// left after inserting one into another ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function for comparision ` `bool` `comp(` `const` `pair<` `int` `, ` `int` `>& L, ` `const` `pair<` `int` `, ` `int` `>& R) ` `{ ` ` ` `if` `(L.first == R.first) ` ` ` `return` `L.second > R.second; ` ` ` ` ` `return` `L.first < R.first; ` `} ` ` ` `// Function to find the minimum number of rectangles ` `// left after inserting one into another ` `int` `Rectangles(pair<` `int` `, ` `int` `> rectangle[], ` `int` `n) ` `{ ` ` ` `// Sort rectangles in increasing order of width ` ` ` `// and decreasing order of height ` ` ` `sort(rectangle, rectangle + n, comp); ` ` ` ` ` `vector<pair<` `int` `, ` `int` `> > nested; ` ` ` ` ` `// Keep the largest rectangle ` ` ` `nested.push_back(rectangle[n - 1]); ` ` ` ` ` `// For all remaining rectangles ` ` ` `for` `(` `int` `i = n - 2; i >= 0; --i) { ` ` ` `int` `high = nested.size() - 1, low = 0; ` ` ` ` ` `// Fidn the position of this rectangle in nested ` ` ` `while` `(low <= high) { ` ` ` `int` `mid = (high + low) / 2; ` ` ` `if` `(nested[mid].first == rectangle[i].first ` ` ` `|| nested[mid].second <= rectangle[i].second) ` ` ` `low = mid + 1; ` ` ` ` ` `else` ` ` `high = mid - 1; ` ` ` `} ` ` ` ` ` `// If this rectangle not possible to insert in ` ` ` `// any other rectangle ` ` ` `if` `(low == nested.size()) ` ` ` `nested.push_back(rectangle[i]); ` ` ` ` ` `// Replace with previous rectangle ` ` ` `else` `{ ` ` ` `nested[low].second = rectangle[i].second; ` ` ` `nested[low].first = rectangle[i].first; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `((` `int` `)nested.size()); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// list of Width, Height pair ` ` ` `pair<` `int` `, ` `int` `> arr[] = { { 20, 30 }, { 10, 10 }, { 30, 20 }, { 40, 50 } }; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << Rectangles(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Find the number of points that have atleast 1 point above, below, left or right of it
- Find if two rectangles overlap
- Number of rectangles in N*M grid
- Minimum flips to make all 1s in left and 0s in right | Set 2
- Find minimum number to be divided to make a number a perfect square
- Number of unique rectangles formed using N unit squares
- Find a number which give minimum sum when XOR with every number of array of integers
- Minimum flips to make all 1s in left and 0s in right | Set 1 (Using Bitmask)
- Find minimum sum of factors of number
- Find minimum possible digit sum after adding a number d
- Find minimum number of coins that make a given value
- Find the minimum number of steps to reach M from N
- Find minimum number of Log value needed to calculate Log upto N
- Find row with maximum and minimum number of zeroes in given Matrix

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.