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

The Strange Function

One of the most important skills a programmer needs to learn early on is the ability to pose a problem in an abstract way. This skill is important not just for researchers but also in applied fields like software engineering and web development. You are able to solve most of a problem, except for one last subproblem, which you have posed in an abstract way as follows: Given an array consisting

View Solution →

Self-Driving Bus

Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever

View Solution →

Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

View Solution →

Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

View Solution →

Pair Sums

Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v

View Solution →

Lazy White Falcon

White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi

View Solution →