Problem: A, B, C, D are lanes with capacity of 10 slots each.
Input Format:
i) Point 1: First take the inputs for the 4 lanes with booked slots 1-10 for each Lane (4 inputs in 4 lines and the values are separated by commas)
ii) Point 2: If no slot is booked for a lane ‘-1’ will be given as an input for that lane.
iii) Point 3: At 5th line take input for number of waiting cars.
To perform:
Point 1: For all waiting cars to be parked first find out the lane with maximum free slots if 2 lane have the same number of highest free slots, then prefer A -> D flow.
Point 2: Fill each waiting car, in the selected lane and mark it as the sequence number for the lane with free slots A, A2,…….A10 till all waiting cars are parked. If A has already filled 5 slots then fill waiting slots from A6-A10
Point 3: If the waiting cars are not completely filled, then fill the next highest free lane continue till all waiting cars get parked.
Note
If no slot is free and cars can’t be parked then print capital ‘X‘ (without quotes).
Sample Input:
A1,A2,A3,A4
B1,B2,B3
C1,C2
D1,D2,D3,D4,D5
10
Sample Output:
C3 C4 C5 C6 C7 C8 C9 C10 B4 B5
Explanation: Here the Slot C has the highest number of available spaces (10 (total space) – 2 (Occupied spaces) = 8 (left out spaces) ). So first 8 cars out of 10 cars in the waiting list are parked in the slot, the remaining 2 cars are parked into Slot B, which is the second highest slot with available spaces (10-3 = 7).
Solution: We strongly recommend you to try the problem first before moving to the solution.
Python
# Python code for Lane Problem
# www.codewindow.in
def returnMaxFreeSlotsLane(free_slots):
lane_no = 1
free = free_slots[1]
for i in range(2, 5):
if(free < free_slots[i]):
free = free_slots[i]
lane_no = i
return lane_no
lane = ["T", "A", "B", "C", "D"]
parked_car = []
free_slots = 10 * [5]
free_slots[0] = 0
for i in range(1, 5):
booked_slots = input().split(",")
if(booked_slots[0] != '-1'):
booked = len(booked_slots)
free_slots[i] = 10 - booked
waiting_cars = int(input())
if(sum(free_slots) < waiting_cars):
print("X")
JAVA
/* JAVA solution for Lane problem */
/* www.codewindow.in */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
class ParkingSlot {
static int returnMaxFreeSlotLane(int freeSlots[]) {
int laneNo = 1;
int free = freeSlots[1];
for(int i = 2 ; i <= 4 ; i++) {
if(free < freeSlots[i]) {
free = freeSlots[i];
laneNo = i;
}
}
return laneNo;
}
static int sum(int freeSlots[]) {
int freeSlotsSum = 0;
for(int i = 1 ; i <= 4 ; i++) {
freeSlotsSum += freeSlots[i];
}
return freeSlotsSum;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int freeSlots [] = { 0, 10, 10, 10, 10 };
String lane[] = { "T", "A", "B", "C", "D" };
ArrayList<String> parkedCar = new ArrayList<>();
for(int i = 1 ; i <= 4 ; i++) {
String bookedSlots [] = br.readLine().split(",");
if(bookedSlots[0].equals("-1") == false) {
int booked = bookedSlots.length;
freeSlots[i] = (10 - booked);
}
}
int waitingCars = Integer.parseInt(br.readLine());
if(sum(freeSlots) < waitingCars) {
System.out.println("X");
return;
}
while(waitingCars != 0) {
int laneNo = returnMaxFreeSlotLane(freeSlots);
int booked = 10 - (freeSlots[laneNo]);
while(waitingCars != 0 && booked != 10) {
booked++;
parkedCar.add(lane[laneNo]+booked);
waitingCars -= 1;
}
freeSlots[laneNo] = 10 - booked;
}
for(String booked : parkedCar)
System.out.print(booked+" ");
}
}
Input:
A1,A2,A3,A4
B1,B2,B3
C1,C2
D1,D2,D3,D4,D5
10
Output:
C3 C4 C5 C6 C7 C8 C9 C10 B4 B5
Follow Us
You Missed
- Unlocking Innovation and Diversity: Accenture HackDiva Empowers Women in Tech with Cutting-Edge Solutions – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- Software Engineer Positions at Siemens Company: Apply Now – codewindow.in
- Cloud Engineer II Opportunities at Insight Company: Apply Now – codewindow.in
- Shape Your Career: Assistant Engineer Opportunities at Jindal Company – codewindow.in
- Shape Your Future: Executive Opportunities at Jindal Company – cdewindow.in
- Associate Engineer, Software Development at Ingram: Apply Now – codewindow.in
- Jade Company’s UI/UX Development Engineer Opportunities – Apply Now – codewindow.in
- Transform Your Career with S&P Global: Apply for the Software Development Engineer Role and Lead the Future of Financial Technology Innovation – codewindow.in
- Unlock Your Potential at Accenture as an Associate Software Engineer – Elevate Your Career with Innovation and Excellence – codewindow.in
- Accelerate Your Career: Join NVIDIA’s Elite Software Engineering Internship Program and Shape the Future of Technology – codewindow.in
- C Programming Interview Questions – codewindow.in
- Lead the Way in Analytics: Specialist Position at Razorpay – codewindow.in
- Innovate with Cyient: Junior Software Engineer Wanted – codewindow.in
- Innovate with Volvo: Associate Software Engineer Wanted – codewindow.in
- Lead the Tech Revolution: Full Stack Developer at Unisys – codewindow.in
- Software Engineer at ABB: Unlock Innovation and Shape the Future – codewindow.in
- IBM Associate Systems Engineer Job: Boost Your Career with a Leading Technology Giant – codewindow.in
- Make Your Mark in Android Development: Join Concentrix – codewindow.in
- Infosys is Growing: Field Services Developer Role Now Open – codewindow.in
- Start Your IT Career Journey with Amazon: IT Services Support Associate I Opportunity – codewindow.in
- Shape the Future of Web: Front-End Software Engineer Opportunity at Google Cloud – codewindow.in
- Barclays QA Team Expands: QA Analyst Role Now Open- codewindow.in
- Eurofins QA Team Grows: Test Engineer Role Now Open – codewindow.in
- Exciting Opportunity: Java Spring Boot Senior Developer Role at Infosys – codewindow.in
- Unlock Your Potential at Nokia: Software Engineer Opportunities Await – codewindow.in
- Join Microsoft’s World-Class Team as a Software Engineer and Shape the Future of Technology – codewindow.in
- Virtusa is Seeking Talented React JS Developers to Drive Digital Excellence – codewindow.in
- Join IBM Dynamic Team as a Full Stack Developer and Shape the Future – codewindow.in
- EY Welcomes Aspiring AI/ML Interns to Unlock the Future of -codewindow.in
- Exciting Opportunity: Project Engineer at Rockwell Automation- codewindow.in