There are n applicants and m 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 n, int m, int k,vector<int> &a, vector<int> &b){sort(a.begin(), a.end());sort(b.begin(), b.end());int ans = 0;for (int i = 0, j = 0; i < n && j < m;){if (a[i] + k < b[j])i++;else if (a[i] - k > b[j])j++;elseans++, i++, j++;}cout << ans << "\n";}int main(){int n = 4, m = 3, k = 5;vector<int> a = {60, 45, 80, 60};vector<int> b = {30, 60, 75};apartments(n, m, k, a, b);return 0;}
No comments:
Post a Comment