Problem Description
Dr. Vishnu is opening a new world-class hospital in a small town designed to be the first preference of the patients in the city. Hospital has N rooms of two types with TV and without TV, with daily rates of R1 and R2 respectively. However, from his experience Dr. Vishnu knows that the number of patients is not constant throughout the year, instead, it follows a pattern. The number of patients on any given day of the year is given by the following formula:
Patients per day = (6 – M)2 + |D – 15|
where,
M: the number of month starting from 1 to 12
D: the date from 1 to 31
All patients prefer without TV rooms as they are cheaper, but will opt for with TV rooms only if without TV rooms are not available. Hospital has a revenue target for the first year of operation. Given this target and the values of N, R1, and R2 you need to identify the number of TVs the hospital should buy so that it meets the revenue target. Assume the Hospital opens on 1st Jan and year is a non-leap year.
Examples:
Input: N = 20, R1 = 1500, R2 = 1000, target = 7000000
Output: 14
Explanation:
Using the formula:
The number of patients on 1st Jan will be 39, on 2nd Jan will be 38 and so on.
Considering there are only twenty rooms and rates of both type of rooms are 1500 and 1000 respectively.
Therefore, 14 TV sets are needed to get the revenue of 7119500 with 13 TV sets.
The total revenue will be less than 7000000.Input: N = 10, R1 = 1000, R2 = 1500, target = 10000000
Output: 10
Explanation:
In the above example, the target will not be achieved, even by equipping all the rooms with TV.
Hence, the answer is 10 i.e., total number of rooms in the hospital.
Approach: The idea is to traverse through the whole year and generate revenue for each day. Find the revenue for the first year for every possible number of rooms that can have a TV. Follow the below steps to solve the problem:
- Suppose the total number of rooms having a TV is tvs, where 0 ? tvs ? N.
- For each number for TVs, revenue for the first year can be found by traversing through each day.
- The above formula can be used to find the number of patients per day. Suppose np is the current number of patients.
- The current number of patients np can have the value of the number of patients today or the total number of rooms whichever is minimum.
- If the number of patients is less than the number of rooms without TV, then the total revenue for today will be np*R2 because it is cheap.
- Else if the number of patients is greater than the rooms without TV, then the total revenue for today is given by:
(N – tvs)*R2 + (np – (N – tvs))*R1
- Add the revenue for each day in target value and print the maximum target value generated after checking all possible combinations.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function that returns number of // patient for a day in a month int getPatients( int M, int D) { return ((6 - M) * (6 - M)) + abs (D - 15); } // Function that count the TVs with // given amount of revenue target void countTVs( long long n, long long r1, long long r2, long long target) { long long np, tvs, current_target; // Days in each month vector< int > days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // Check all possible combinations for (tvs = 0; tvs <= n; tvs++) { // Stores the current target current_target = 0; for ( int m = 1; m <= 12; m++) { for ( int d = 1; d <= days[m]; d++) { // Number of patients // on day d of month m np = getPatients(m, d); // Patients cannot be // exceed number of rooms np = min(np, n); // If the number of patient is // <= count of rooms without tv if (np <= n - tvs) { // All patients will opt // for rooms without tv current_target += np * r2; } // Otherwise else { // Some will opt for // rooms with tv and // others without tv current_target += ((n - tvs) * r2 + ((np - (n - tvs)) * r1)); } } } // If current target meets // the required target if (current_target >= target) { break ; } } // Print the count of TVs cout << min(tvs, n); } // Driver Code int main() { long long N = 20, R1 = 1500; long long R2 = 1000; long long target = 7000000; // Function Call countTVs(N, R1, R2, target); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG { // Function that returns number of // patient for a day in a month static int getPatients( int M, int D) { return (( 6 - M) * ( 6 - M)) + Math.abs(D - 15 ); } // Function that count the TVs with // given amount of revenue target static void countTVs( int n, int r1, int r2, int target) { int np, tvs, current_target; // Days in each month int [] days = { 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 }; // Check all possible combinations for (tvs = 0 ; tvs <= n; tvs++) { // Stores the current target current_target = 0 ; for ( int m = 1 ; m <= 12 ; m++) { for ( int d = 1 ; d <= days[m]; d++) { // Number of patients // on day d of month m np = getPatients(m, d); // Patients cannot be // exceed number of rooms np = Math.min(np, n); // If the number of patient is // <= count of rooms without tv if (np <= n - tvs) { // All patients will opt // for rooms without tv current_target += np * r2; } // Otherwise else { // Some will opt for // rooms with tv and // others without tv current_target += ((n - tvs) * r2 + ((np - (n - tvs)) * r1)); } } } // If current target meets // the required target if (current_target >= target) { break ; } } // Print the count of TVs System.out.print(Math.min(tvs, n)); } // Driver Code public static void main(String[] args) { int N = 20 , R1 = 1500 ; int R2 = 1000 ; int target = 7000000 ; // Function Call countTVs(N, R1, R2, target); } } // This code is contributed by phasing17. |
Python3
# Python3 program for the above approach # Function that returns number of # patient for a day in a month def getPatients( M, D): return (( 6 - M) * ( 6 - M)) + abs (D - 15 ); # Function that count the TVs with # given amount of revenue target def countTVs( n, r1, r2, target): # Days in each month days = [ 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ]; # Check all possible combinations for tvs in range (n + 1 ): # Stores the current target current_target = 0 ; for m in range ( 1 , 13 ): for d in range ( 1 , 1 + days[m]): # Number of patients # on day d of month m np = getPatients(m, d); # Patients cannot be # exceed number of rooms np = min (np, n); # If the number of patient is # <= count of rooms without tv if (np < = n - tvs) : # All patients will opt # for rooms without tv current_target + = np * r2; # Otherwise else : # Some will opt for # rooms with tv and # others without tv current_target + = ((n - tvs) * r2 + ((np - (n - tvs)) * r1)); # If current target meets # the required target if (current_target > = target): break ; # Pr the count of TVs print ( min (tvs, n)); # Driver Code N = 20 R1 = 1500 ; R2 = 1000 ; target = 7000000 ; # Function Call countTVs(N, R1, R2, target); # This code is contributed by phasing17. |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG { // Function that returns number of // patient for a day in a month static int getPatients( int M, int D) { return ((6 - M) * (6 - M)) + Math.Abs(D - 15); } // Function that count the TVs with // given amount of revenue target static void countTVs( int n, int r1, int r2, int target) { int np, tvs, current_target; // Days in each month int [] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // Check all possible combinations for (tvs = 0; tvs <= n; tvs++) { // Stores the current target current_target = 0; for ( int m = 1; m <= 12; m++) { for ( int d = 1; d <= days[m]; d++) { // Number of patients // on day d of month m np = getPatients(m, d); // Patients cannot be // exceed number of rooms np = Math.Min(np, n); // If the number of patient is // <= count of rooms without tv if (np <= n - tvs) { // All patients will opt // for rooms without tv current_target += np * r2; } // Otherwise else { // Some will opt for // rooms with tv and // others without tv current_target += ((n - tvs) * r2 + ((np - (n - tvs)) * r1)); } } } // If current target meets // the required target if (current_target >= target) { break ; } } // Print the count of TVs Console.Write(Math.Min(tvs, n)); } // Driver Code public static void Main( string [] args) { int N = 20, R1 = 1500; int R2 = 1000; int target = 7000000; // Function Call countTVs(N, R1, R2, target); } } // This code is contributed by phasing17. |
Javascript
// JS program for the above approach // Function that returns number of // patient for a day in a month function getPatients( M, D) { return ((6 - M) * (6 - M)) + Math.abs(D - 15); } // Function that count the TVs with // given amount of revenue target function countTVs( n, r1, r2, target) { let np, tvs, current_target; // Days in each month let days = [ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]; // Check all possible combinations for (tvs = 0; tvs <= n; tvs++) { // Stores the current target current_target = 0; for ( var m = 1; m <= 12; m++) { for ( var d = 1; d <= days[m]; d++) { // Number of patients // on day d of month m np = getPatients(m, d); // Patients cannot be // exceed number of rooms np = Math.min(np, n); // If the number of patient is // <= count of rooms without tv if (np <= n - tvs) { // All patients will opt // for rooms without tv current_target += np * r2; } // Otherwise else { // Some will opt for // rooms with tv and // others without tv current_target += ((n - tvs) * r2 + ((np - (n - tvs)) * r1)); } } } // If current target meets // the required target if (current_target >= target) { break ; } } // Pr the count of TVs console.log(Math.min(tvs, n)); } // Driver Code let N = 20, R1 = 1500; R2 = 1000; target = 7000000; // Function Call countTVs(N, R1, R2, target); // This code is contributed by phasing17. |
14
Time Complexity: O(N*365), where N is the given number of rooms.
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!