# 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)```
```

## Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

## Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

## Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

## Is This a Binary Search Tree?

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a

## Square-Ten Tree

The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the

## Balanced Forest

Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a