Lane Problem- Infytq 2019 Solve

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


Leave a Comment

Your email address will not be published. Required fields are marked *