Minimize rooms with K seats to accomodate N girls and M boys
Given three positive integers N, M, and K, the task is to find the minimum number of rooms required to accommodate all students if there are N girls and M boys, and each room has K seats. It is not allowed for a boy and a girl to stay in the same room.
Examples:
Input: N = 13, M = 7, K = 2
Output: 11
Explanation: Room required for girls = 7 (2 + 2 + 2 + 2 + 2 + 2 + 1) Room required for boys = 4 (2 + 2 + 2 + 1)Input: N = 5, M = 5, K = 3
Output: 4
Explanation: Rooms required for girls = 2 (3 + 2) Room required for boys = 2 (3 + 2). So, we output total seats 2 + 2 = 4
Approach: This can be solved with the following idea:
This can be solved by mathematical observation. Calculating rooms required for boys and girls separately.
Steps involved in the implementation of code:
- If the number of students in a particular gender is exactly divisible by the number of seats in a room, then the number of rooms required for that gender can be calculated as the integer division of the number of students in that gender by the number of seats in a room.
- If the number of students in a particular gender is not exactly divisible by the number of seats in a room, then the number of rooms required for that gender can be calculated as the integer division of the number of students in that gender by the number of seats in a room, plus one. This is because an additional room is required to accommodate the remaining students.
- Steps 1 and 2 can be applied separately for boys and girls to calculate the minimum number of rooms required for each gender.
- The minimum number of rooms required to accommodate all students can be calculated as the maximum of the minimum number of rooms required for boys and girls, i.e., answer = max(ceil(M/K), ceil(N/K)).
- Finally, return the answer as the minimum number of rooms required to accommodate all students.
Below is the implementation of the code:
C++14
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Given three positive integers N, M, and K, the task is to find the minimum number of rooms required to accommodate all students if there are N girls and M boys, and each room has K seats. It is not allowed for a boy and a girl to stay in the same room.
Examples:
Input: N = 13, M = 7, K = 2
Output: 11
Explanation: Room required for girls = 7 (2 + 2 + 2 + 2 + 2 + 2 + 1) Room required for boys = 4 (2 + 2 + 2 + 1)Input: N = 5, M = 5, K = 3
Output: 4
Explanation: Rooms required for girls = 2 (3 + 2) Room required for boys = 2 (3 + 2). So, we output total seats 2 + 2 = 4
Approach: This can be solved with the following idea:
This can be solved by mathematical observation. Calculating rooms required for boys and girls separately.
Steps involved in the implementation of code:
- If the number of students in a particular gender is exactly divisible by the number of seats in a room, then the number of rooms required for that gender can be calculated as the integer division of the number of students in that gender by the number of seats in a room.
- If the number of students in a particular gender is not exactly divisible by the number of seats in a room, then the number of rooms required for that gender can be calculated as the integer division of the number of students in that gender by the number of seats in a room, plus one. This is because an additional room is required to accommodate the remaining students.
- Steps 1 and 2 can be applied separately for boys and girls to calculate the minimum number of rooms required for each gender.
- The minimum number of rooms required to accommodate all students can be calculated as the maximum of the minimum number of rooms required for boys and girls, i.e., answer = max(ceil(M/K), ceil(N/K)).
- Finally, return the answer as the minimum number of rooms required to accommodate all students.
Below is the implementation of the code:
C++14
|
Time Complexity: O(1)
Auxiliary Space: O(1)