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 :



title-img




                        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

Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →

Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →