Grid Value
You are given a grid of size N*M. There are two types of people, type ‘A’ and type ‘B’ . You are also given the number of people of each type say ‘countA’ is the number of ‘A’ type persons and ‘countB’ is the number of ‘B’ type of persons. The satisfaction of a particular type of person can increase or decrease as follows :
The initial satisfaction of a person of type A is 80. It decreases by 20 for each neighbor.
The initial satisfaction of a person of type B is 50. It increases by 10 for each neighbor.
Your task is to determine the maximum possible grid satisfaction.
Note:
You may choose exactly how many people you want to be present in the grid.
The total number of people of type ‘A’ living in the grid can be less than ‘countA’ but cannot exceed ‘countA’. Similarly, total number of people of type ‘B’ living in the grid can be less than ‘countB’ but cannot exceed ‘countB’
A person can live in only one cell.
Not more than one person can live in a cell.
Two cells are said to be neighbors if the cells are adjacent and share a boundary.
Input Format:
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the ‘T’ test cases follow.
The first and the only line of each test case contains four space-separated integers ‘N’, ‘M’, ‘countA’, and ‘countB’, denoting the grid’s dimensions and the number of each type of people present.
Output Format:
For each test case, print in a new line an integer denoting the maximum possible grid satisfaction.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 2
1 <= N,M <= 6
0 <= countA, countB <= min(N*M, 6)
Time Limit: 1sec
CodingNinjas
author
2y
Using Recursion
The approach is to use Recursion along with Bitmasking. We can observe that we only need to know the last M cells we processed. We should only consider the cells at the top and left of ...read more
CodingNinjas
author
2y
Dynamic Programming with Bitmasking
The approach is to use Dynamic Programming along with Bitmasking. We can observe that we only need to know the last M cells we processed. We should only consider the...read more
Help your peers!
Add answer anonymously...
Top Intuit Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app