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
Jesse and Cookies
Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t
View Solution →Find the Running Median
The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.
View Solution →Minimum Average Waiting Time
Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h
View Solution →Merging Communities
People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w
View Solution →Components in a graph
There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu
View Solution →Kundu and Tree
Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that
View Solution →