
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find out how many transfer requests can be satisfied in Python
Suppose, there are n number of hostel rooms numbered from 0 to n-1. The students in the hostel rooms want to transfer to another room, and they place several requests to do that. No hostel seat remains vacant, a transfer request is only taken care of if another student takes the place of the student willing to transfer. So, given the requests, we have to find out how many requests can be satisfied.
So, if the input is like n = 3, requests = [[0,2],[1,0],[2,1]], then the output will be 3.
The student in room 0 transfers to room 2.
The student in room 1 transfers to room 0.
The student in room 2 transfers to room 1.
To solve this, we will follow these steps −
for k in range size of requests to -1, decrease by 1, do
for c in all combinations of (0 to size of requests and k), do
d := a new array of size n containing value 0
for each i in c, do
d[requests[i, 0]] := d[requests[i, 0]] - 1
d[requests[i, 1]] := d[requests[i, 1]] + 1
if none of the items in d are true, then
return k
return 0
Example
Let us see the following implementation to get better understanding
from itertools import combinations def solve(n, requests): for k in range(len(requests), 0, -1): for c in combinations(range(len(requests)), k): d = [0] * n for i in c: d[requests[i][0]] -= 1 d[requests[i][1]] += 1 if not any(d): return k return 0 print(solve(3, [[0,2],[1,0],[2,1]]))
Input
3, [[0,2],[1,0],[2,1]]
Output
3
- Related Articles
- Program to find out how many boxes can be put into the godown in Python
- Python Program to find out how many cubes are cut
- Program to find out number of blocks that can be covered in Python
- How can transfer learning be implemented in Python using Keras?
- Program to find how many ways we can climb stairs in Python
- Program to find maximum how many water bottles we can drink in Python
- Python Program to find out the number of rooms in which a prize can be hidden
- Python Program to find out how many times the balls will collide in a circular tube
- How can you have a satisfied life?
- Program to find how many total amount of rain we can catch in Python
- Program to find how many lines intersect in Python
- C++ Program to find out how many movies an attendee can watch entirely at a Film festival
- Program to check number of requests that will be processed with given conditions in python
- Program to count how many times we can find "pizza" with given string characters in Python
- Program to Find Out Currency Arbitrage in Python
