Q1: Assigning Patients to Preferred Slots
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
bool canAssignSlots(const vector<int>& A, const vector<int>& B, int S) {
unordered_set<int> assignedSlots;
for (int i = 0; i < A.size(); ++i) {
// Check if the first preference can be assigned
if (assignedSlots.find(A[i]) == assignedSlots.end()) {
assignedSlots.insert(A[i]);
}
// Check if the second preference can be assigned
else if (assignedSlots.find(B[i]) == assignedSlots.end()) {
assignedSlots.insert(B[i]);
}
// If neither preference can be assigned, return false
else {
return false;
}
}
return true;
}
int main() {
// Example Usage
vector<int> A = {1, 2, 3};
vector<int> B = {4, 5, 6};
int S = 6;
if (canAssignSlots(A, B, S)) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
return 0;
}
2. the Python implementation is:
python
def max_size_squares(matrix):
m, n = len(matrix), len(matrix[0])
# Calculate the side length of the square submatrix ending at each cell
for i in range(1, m):
for j in range(1, n):
if matrix[i][j] == 1:
matrix[i][j] = min(matrix[i-1][j], matrix[i][j-1], matrix[i-1][j-1]) + 1
# Find the two largest non-overlapping squares
max1, max2 = 0, 0
for i in range(m):
for j in range(n):
if matrix[i][j] > max1:
max2 = max1
max1 = matrix[i][j]
elif matrix[i][j] > max2:
max2 = matrix[i][j]
return max1 * max2
# Example
matrix = [
[1, 1, 0, 1],
[1, 1, 1, 1],
[0, 1, 1, 0],
[1, 1, 1, 1]
]
result = max_size_squares(matrix)
print(result)