Package Matching - Google Top Interview Questions


Problem Statement :


You are given two-dimensional list of integers sales and buyers. 

Each element in sales contains [day, price] meaning that the package is only available for sale on that day for that price. 

Each element in buyers contains [payday, amount] meaning that the buyer has that amount of money to spend on payday and afterwards.

Given that each buyer can buy at most one package, and each package can be sold to at most one person, return the maximum number of packages that can be bought.

Constraints

n ≤ 100,000 where n is the length of buyers

m ≤ 100,000 where m is the length of sales

Example 1

Input

sales = [

    [0, 2],

    [0, 2],

    [0, 3],

    [1, 1],

    [1, 2],

    [3, 1]

]

buyers = [

    [0, 1],

    [0, 3],

    [1, 2]

]

Output

3

Explanation

The first person can take [1, 1] package. The second person can take the [0, 3] package. The last 
person can take the [1, 2] package.



Solution :



title-img




                        Solution in C++ :

int solve(vector<vector<int>>& sales, vector<vector<int>>& buyers) {
    sort(buyers.begin(), buyers.end());
    sort(sales.begin(), sales.end());
    multiset<int> m;
    int i = sales.size() - 1, j = buyers.size() - 1, ans = 0;
    while (j >= 0) {
        while (i >= 0 and sales[i][0] >= buyers[j][0]) m.insert(sales[i--][1]);
        int p = buyers[j--][1];
        if (!m.empty()) {
            auto x = m.upper_bound(p);
            if (x-- != m.begin()) {
                m.erase(x);
                ans += 1;
            }
        }
    }
    return ans;
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    void add(TreeMap<Integer, Integer> tm, int val) {
        tm.put(val, tm.getOrDefault(val, 0) + 1);
    }
    void remove(TreeMap<Integer, Integer> tm, int val) {
        tm.put(val, tm.get(val) - 1);
        if (tm.get(val) == 0)
            tm.remove(val);
    }

    public int solve(int[][] sales, int[][] buyers) {
        Arrays.sort(sales, (a, b) -> (a[0] - b[0]));
        Arrays.sort(buyers, (a, b) -> (a[0] - b[0]));
        int n = sales.length - 1, m = buyers.length - 1, res = 0;
        TreeMap<Integer, Integer> tm = new TreeMap();
        while (m >= 0) {
            int payday = buyers[m][0], amount = buyers[m][1];
            while (n >= 0 && sales[n][0] >= payday) {
                add(tm, sales[n][1]);
                n--;
            }
            Integer best_price = tm.floorKey(amount);
            if (best_price != null) {
                remove(tm, best_price);
                res++;
            }
            m--;
        }
        return res;
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, sales, buyers):
        sales.sort()
        buyers.sort()

        ans = j = 0
        offers = SortedList()
        for d, p in sales:
            while j < len(buyers) and buyers[j][0] <= d:
                offers.add(buyers[j][1])
                j += 1

            index = offers.bisect_left(p)
            if index < len(offers):
                offers.pop(index)
                ans += 1

        return ans
                    


View More Similar Problems

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →

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 →