Apartments

There are n applicants and free apartments. Your task is to distribute the apartments so that as many applicants as possible will get an apartment.

Each applicant has the desired apartment size, and they will accept any apartment whose size is close enough to the desired size.

Example:

Input:  n = 4, m = 3, k = 5, a = {60, 45, 80, 60}, b = {30, 60, 75}
Output: 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void apartments(int nint mint k,
                vector<int&avector<int&b)
{

    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int ans = 0;
    for (int i = 0j = 0i < n && j < m;)
    {
        if (a[i] + k < b[j])
            i++;
        else if (a[i] - k > b[j])
            j++;
        else
            ans++, i++, j++;
    }
    cout << ans << "\n";
}

int main()
{
    int n = 4m = 3k = 5;

    vector<inta = {60458060};
    vector<intb = {306075};

    apartments(nmkab);

    return 0;
}


No comments:

Post a Comment