In this HackerEarth Elevator Overload problem solution, A corporate building has n floors numbered from 1 to n. An elevator starts moving from floor 1 and goes upward until it reaches floor the n. It has the maximum capacity of weight (W) and persons (P) it can lift. It will stop at every floor and there will be some number of employees who will get into the elevator and also some of the employees will leave it as they have reached their desired floors. On every floor, those who have reached the desired floor will leave first, and then those who were waiting for the elevator will get in. As the elevator has a maximum limit of weight and persons (when any one of the limits is exceeded, the overload sign is displayed), it will permanently stop where the overload situation happens or it will permanently stop at floor n if no overload situation happens. You need to find the floor, where the elevator will stop permanently.

Note: If an employee gets in at floor x, then it is guaranteed that his desired floor will be greater than x and less than or equal to n. There are no employees waiting at floor n. You have to answer for t number of test cases.


HackerEarth Elevator Overload problem solution


HackerEarth Elevator Overload problem solution.

from collections import *

#Class used for keeping track of employees present in the elevator
class desiredFloor:
    def __init__(self):
        #number of employees
        self.noe = 0
        #total weight
        self.wt = 0

for _ in range(input()):
    #number of floors
    n = input()
    #Maximum Limit of Persons and Weight
    p,w = map(int,raw_input().strip().split())
    #Number of employees waiting at at each floor
    arr = map(int,raw_input().strip().split())
    d = defaultdict(list)
    for i in range(1,n):
        temp_floor = map(int,raw_input().strip().split())
        temp_wt = map(int,raw_input().strip().split())
        for j in range(arr[i-1]):
            d[i].append([temp_floor[j],temp_wt[j]])
    
    elev = defaultdict(desiredFloor)
    total_wt = 0
    total_employees = 0

    for i in range(1,n+1):
        #Removing weight of employees leaving
        total_wt -= elev[i].wt
        total_employees -= elev[i].noe 
        for emp in d[i]:
            elev[emp[0]].noe += 1
            elev[emp[0]].wt  += emp[1]
            total_wt += emp[1]
            total_employees += 1
        #Checking for the Overload
        if total_employees>p or total_wt>w:
            break
    print i


Second solution

#include <bits/stdc++.h>
#define nax 1005
using namespace std;

int arr[nax],prsn[nax],fl[nax],wt[nax];

long long W[nax],P[nax],Mwt,Mprsn;
vector<int> out[nax];
int main()
{
    int t,n;
    cin>>t;
    assert(t<=10 && t>=1);
    while(t--){
    memset(W,0,sizeof(W));
    memset(P,0,sizeof(P));
    
        cin>>n>>Mprsn>>Mwt;
    assert(n<=1000 && n>=2);
    assert(Mprsn<=100000 && Mprsn>=1);
    assert(Mwt<=10000000 && Mwt>=1);

    for(int i=1;i<n;i++)
    {
    cin>>prsn[i];
    assert(prsn[i]<=100 && prsn[i]>=1);
    }
    W[0]=0;
    int ans = n;
    for(int i=1;i<n;i++)
    {
        int p = prsn[i];
        for(int j=1;j<=p;j++)
        {
            cin>>fl[j];
            assert(fl[j]>i);
        }
        for(int j=1;j<=p;j++)
        {
           cin>>wt[i];
            assert(wt[i]<=100 && wt[i]>=1);
           W[i]+=wt[i];
           P[i]+=1;
           W[fl[j]]-=wt[i];
           P[fl[j]]-=1;
           
        }
        
        W[i]+=W[i-1];
        P[i]+=P[i-1]; 
            
        
    }
    for(int i=1;i<n;i++)
    {
        if(W[i]>Mwt || P[i]>Mprsn)
        {
            ans=i;
            break;
        }
    }
    
    cout<<ans<<"\n";
    
    
    }
    return 0;
}