A corporate building has n floors numbered from 1 to n. An elevator starts moving from the 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. At 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 on floor n.
Example:
Input: n = 4, personMax = 3, weightMax = 150, arr = {2, 2, 2}, floorData = {{2, 3}, {50, 50}, {3, 4}, {50, 50}, {4, 4}, {20, 10}}
Output: 4
Approach
C++
#include <bits/stdc++.h>using namespace std;void elevatorOverloaded(int n, int personMax,int weightMax,vector<int> &arr,vector<vector<int>> &floorData){vector<int> weight_start(n + 1, 0);vector<int> weight_end(n + 1, 0);vector<int> p(n + 1, 0);vector<int> p_end(n + 1, 0);int ans = n;int currPerson = 0, currWeight = 0;for (int i = 1; i < n; i++){p[i] = arr[i - 1];}for (int i = 1; i <= n; i++){vector<int> floor;for (int j = 0; j < p[i]; j++){int x = floorData[j][0];floor.push_back(x);p_end[x]++;}for (int j = 0; j < p[i]; j++){int x = floorData[j][1];weight_end[floor[j]] += x;weight_start[i] += x;}}for (int i = 1; i <= n; i++){currPerson += p[i] - p_end[i];currWeight += weight_start[i] - weight_end[i];if (currPerson > personMax || currWeight > weightMax){ans = i;break;}}cout << ans << "\n";}int main(){int n = 4;int personMax = 3, weightMax = 150;vector<int> arr = {2, 2, 2};vector<vector<int>> floorData = {{2, 3},{50, 50},{3, 4},{50, 50},{4, 4},{20, 10}};elevatorOverloaded(n, personMax, weightMax, arr, floorData);return 0;}
No comments:
Post a Comment