Coding - Chairs Requirement

Rumman Ansari   Software Engineer   2024-04-06 07:23:12   30 Share
Subject Syllabus DetailsSubject Details
☰ Table of Contents

Table of Content:


1. Coding - Chairs Requirement

In this challenge, determine the minimum number of chairs to be purchased to accommodate all workers in a new business workroom. There is no chair at the beginning.

There will be a string array of simulations. Each simulation is described by a combination of four characters: C, R, U, and L

C-A new employee arrives in the workroom. If there is a chair available, the employee takes it. Otherwise, a new one is purchased.

R-An employee goes to a meeting room, freeing up a chair.

U-An employee arrives from a meeting room. If there is a chair available, the employee takes it. Otherwise. a new one is purchased.

L-An employee leaves the workroom, freeing up a chair.

Example
Given an array of strings representing the simulations a = ["CRUL"]
In this case, there is only one simulation, CRUL, represented in the table below:
Action Total Available
- 0 0
C 1 0
R 1 1
U 1 0
L 1 1
• At first, there are 0 chairs.
• "C" : A new employee arrives in the workroom and one chair was purchased.
• "R" : An employee goes to the meeting room, freeing up a chair.
• "U" : An employee arrives from the meeting room, took the chair available
• "L" : An employee leaves the workroom, freeing up a chair.
The minimum number of chairs to be purchased is one chair in this case. result=[1]

 



Function Description

Complete the minChairs function in the editor below.

minChairs has the following parameter(s): string simulations[n]: an array of strings representing discrete simulations to process.

Returns:

int[n]: an array of integers denoting the minimal number of chairs required for each simulation.

 


#!/bin/python3

import math
import os
import random
import re
import sys



#
# Complete the 'minChairs' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts STRING_ARRAY simulations as parameter.
#

def minChairs(simulations):
    chairs_needed_list = []
    
    for sim in simulations:
        chairs_needed = 0
        current_chairs = 0
        for action in sim:
            if action == 'C':
                if current_chairs == 0:
                    chairs_needed += 1
                else:
                    current_chairs -= 1
            elif action == 'R':
                current_chairs += 1
            elif action == 'U':
                if current_chairs == 0:
                    chairs_needed += 1
                else:
                    current_chairs -= 1
            elif action == 'L':
                current_chairs += 1
        chairs_needed_list.append(chairs_needed)
    
    return chairs_needed_list

    # Write your code here

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    simulations_count = int(input().strip())

    simulations = []

    for _ in range(simulations_count):
        simulations_item = input()
        simulations.append(simulations_item)

    result = minChairs(simulations)

    fptr.write('\n'.join(map(str, result)))
    fptr.write('\n')

    fptr.close()