**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 :

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

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

View Solution →## 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

View Solution →## 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

View Solution →## 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

View Solution →## 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

View Solution →## Jenny's Subtrees

Jenny loves experimenting with trees. Her favorite tree has n nodes connected by n - 1 edges, and each edge is ` unit in length. She wants to cut a subtree (i.e., a connected part of the original tree) of radius r from this tree by performing the following two steps: 1. Choose a node, x , from the tree. 2. Cut a subtree consisting of all nodes which are not further than r units from node x .

View Solution →