- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have two lists sales and buyers. Each element in sales contains two values in the form [day, price] this indicates the package is available for sale only on that day for that given price. And each element in buyers in the form [payday, amount] indicates that the buyer has that amount of money to spend on payday and afterward. If each buyer can buy at most one package, and each package can be sold to only one person, find the maximum number of packages that can be bought.

So, if the input is like sales = [[0, 5], [0, 5], [0, 6], [1, 4], [1, 5], [3, 4]] buyers = [[0, 4], [0, 6],[1, 5]], then the output will be 3, as the first person can take package [1, 4]. The second person can take [0, 6]. And the last person can take the package [1, 5].

To solve this, we will follow these steps −

ret := 0

sort the array buyers based on payday, if paydays are same sort them on amount

define a set pq

sort the array sales

i := 0

for each items it in sales −

while (i < size of buyers and buyers[i, 0] <= it[0]), do −

insert buyers[i, 1] into pq

(increase i by 1)

j := index of pq to insert it[i] intp pq and make it sorted

if j is a valid index, then −

(increase ret by 1)

delete jth element from pq

return ret

Let us see the following implementation to get a better understanding −

#include <bits/stdc++.h> using namespace std; class Solution { public: static bool cmp(vector<int>& a, vector<int>& b) { return a[0] == b[0] ? a[1] > b[1] : a[0] < b[0]; } int solve(vector<vector<int>>& sales, vector<vector<int>>& buyers) { int ret = 0; sort(buyers.begin(), buyers.end()); multiset<int> pq; sort(sales.begin(), sales.end(), cmp); int i = 0; for (auto& it : sales) { while (i < buyers.size() && buyers[i][0] <= it[0]) { pq.insert(buyers[i][1]); i++; } auto j = pq.lower_bound(it[1]); if (j != pq.end()) { ret++; pq.erase(j); } } return ret; } }; int solve(vector<vector<int>>& sales, vector<vector<int>>& buyers) { return (new Solution())->solve(sales, buyers); } int main(){ vector<vector<int>> sales = {{0, 5},{0, 5},{0, 6},{1, 4},{1, 5},{3, 4}}; vector<vector<int>> buyers = {{0, 4},{0, 6},{1, 5}}; cout << solve(sales, buyers); }

{{0, 5},{0, 5},{0, 6},{1, 4},{1, 5},{3, 4}}, {{0, 4},{0, 6},{1, 5}}

3

- Related Questions & Answers
- Maximum number of candies that can be bought in C
- Maximum litres of water that can be bought with N Rupees in C++
- Maximum Number of Events That Can Be Attended in C++
- Find maximum number that can be formed using digits of a given number in C++
- Problem to Find Out the Maximum Number of Coins that Can be Collected in Python
- Maximum number of partitions that can be sorted individually to make sorted in C++
- Maximum number of people that can be killed with strength P in C++
- Maximum number of threads that can be created within a process in C
- Program to find maximum units that can be put on a truck in Python
- Maximum number of parallelograms that can be made using the given length of line segments in C++
- Maximum length cycle that can be formed by joining two nodes of a binary tree in C++
- Program to find maximum number of coins we can collect in Python
- Find maximum points which can be obtained by deleting elements from array in C++
- Maximum money that can be withdrawn in two steps in C
- C++ Program to Find Maximum Number of Edge Disjoint Paths

Advertisements