**Boxes All the Way Down - Amazon Top Interview Questions**

### Problem Statement :

You are given a two-dimensional list of integers boxes. Each list contains two integers [width, height] which represent the width and height of a box. Given that you can put a box in another box if both of its width and height are smaller than the other box, return the maximum number of boxes you can fit into a box. You cannot rotate the boxes. Constraints n ≤ 100,000 where n is the length of boxes Example 1 Input matrix = [ [10, 10], [9, 9], [5, 5], [4, 9] ] Output 3 Explanation We can fit the box [5, 5] into [9, 9] which we can fit into the [10, 10] box.

### Solution :

` ````
Solution in C++ :
struct SegTree {
int n;
vector<int> arr;
vector<int> tree;
SegTree(int n1, vector<int>& a) {
n = n1;
arr = a;
int s = 1;
while (s < 2 * n) {
s = s << 1;
}
tree.resize(s);
fill(tree.begin(), tree.end(), 0);
build(0, n - 1, 1);
}
void build(int start, int end, int index) {
if (start == end) {
arr[start] = tree[index];
return;
}
int mid = (start + end) / 2;
build(start, mid, index * 2);
build(mid + 1, end, index * 2 + 1);
tree[index] = max(tree[2 * index], tree[2 * index + 1]);
}
void update(int start, int end, int index, int idx, int value) {
if (start == end) {
arr[start] = max(arr[start], value);
tree[index] = arr[start];
return;
}
int mid = (start + end) / 2;
if (idx <= mid)
update(start, mid, index * 2, idx, value);
else
update(mid + 1, end, index * 2 + 1, idx, value);
tree[index] = max(tree[2 * index], tree[2 * index + 1]);
}
int query(int start, int end, int index, int left, int right) {
if (start > right || end < left) return 0;
if (start >= left && end <= right) return tree[index];
int mid = (start + end) / 2;
int val1 = query(start, mid, index * 2, left, right);
int val2 = query(mid + 1, end, index * 2 + 1, left, right);
return max(val1, val2);
}
};
bool compare(vector<int>& a, vector<int>& b) {
return a[0] < b[0];
}
int solve(vector<vector<int>>& matrix) {
map<int, int> compression;
int n = matrix.size();
for (int i = 0; i < n; i++) {
compression[matrix[i][1]] = 0;
}
int last = 1;
for (auto& i : compression) {
i.second = last++;
}
for (int i = 0; i < n; i++) {
matrix[i][1] = compression[matrix[i][1]];
}
sort(matrix.begin(), matrix.end(), compare);
vector<vector<int>> points;
int lastX = -1;
vector<int> prev = {};
for (int i = 0; i < n; i++) {
if (matrix[i][0] != lastX) {
if (prev.size() != 0) {
points.push_back(prev);
prev.clear();
}
prev.push_back(matrix[i][1]);
lastX = matrix[i][0];
} else {
prev.push_back(matrix[i][1]);
}
}
if (prev.size() != 0) {
points.push_back(prev);
}
vector<int> ans(n + 1, 0);
SegTree sg = SegTree(n + 1, ans);
for (auto i : points) {
vector<int> toUpdate;
for (auto j : i) {
int value = sg.query(0, n, 1, 0, j - 1);
toUpdate.push_back(value + 1);
}
for (int j = 0; j < i.size(); j++) {
sg.update(0, n, 1, i[j], toUpdate[j]);
}
}
return sg.query(0, n, 1, 0, n);
}
```

` ````
Solution in Java :
import java.util.*;
class Solution {
public int solve(int[][] A) {
Arrays.sort(A, (a, b) -> {
if (a[0] == b[0])
return b[1] - a[1];
return a[0] - b[0];
});
int B[] = new int[A.length];
for (int i = 0; i < A.length; i++) {
B[i] = A[i][1];
}
return lis(B);
}
public int lis(int[] A) {
int dp[] = new int[A.length];
Arrays.fill(dp, Integer.MAX_VALUE);
for (int i = 0; i < A.length; i++) {
bin(dp, A[i]);
}
int res = 0;
for (int i = 0; i < dp.length; i++) {
if (dp[i] != Integer.MAX_VALUE)
res = i + 1;
}
return res;
}
public void bin(int dp[], int val) {
int l = 0, r = dp.length - 1;
int index = -1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (val <= dp[mid]) {
index = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
dp[index] = val;
}
}
```

` ````
Solution in Python :
class Solution:
def solve(self, a):
# double strict
a.sort(key=lambda x: [x[0], -x[1]])
ret = []
for w, h in a:
i = bisect_left(ret, h)
ret[i : i + 1] = [h]
return len(ret)
```

## View More Similar Problems

## Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →## Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →## Palindromic Subsets

Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t

View Solution →## Counting On a Tree

Taylor loves trees, and this new challenge has him stumped! Consider a tree, t, consisting of n nodes. Each node is numbered from 1 to n, and each node i has an integer, ci, attached to it. A query on tree t takes the form w x y z. To process a query, you must print the count of ordered pairs of integers ( i , j ) such that the following four conditions are all satisfied: the path from n

View Solution →## Polynomial Division

Consider a sequence, c0, c1, . . . , cn-1 , and a polynomial of degree 1 defined as Q(x ) = a * x + b. You must perform q queries on the sequence, where each query is one of the following two types: 1 i x: Replace ci with x. 2 l r: Consider the polynomial and determine whether is divisible by over the field , where . In other words, check if there exists a polynomial with integer coefficie

View Solution →## Costly Intervals

Given an array, your goal is to find, for each element, the largest subarray containing it whose cost is at least k. Specifically, let A = [A1, A2, . . . , An ] be an array of length n, and let be the subarray from index l to index r. Also, Let MAX( l, r ) be the largest number in Al. . . r. Let MIN( l, r ) be the smallest number in Al . . .r . Let OR( l , r ) be the bitwise OR of the

View Solution →