Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- "use strict";
- var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- Elevator:
- // There is an elevator in a building with M floors.
- // This elevator can take a max of X people at a time or max of total weight Y.
- // Given that a set of people and their weight and the floor they need to stop,
- // how many stops has the elevator taken to serve all the people?
- //Consider the elevator serves in "first come first serve" basis and the order for the queue can not be changed.
- //
- // Example:
- // Let Array A be the weight of each person A = [60, 80, 40].
- // Let Array B be the floors where each person needs to be dropped off B = [2, 3, 5].
- //
- // The building has 5 floors, maximum of 2 persons are allowed in the elevator
- // at a time with max weight capacity being 200. For this example, the elevator would take total
- // of 5 stops in floors: 2, 3, G, 5 and finally G.
- "use strict";
- var A = [50, 80, 40, 120];
- var B = [2, 3, 5, 1];
- var maxWeight = 200;
- var maxCapacity = 2;
- var journeys = undefined;
- // are there more people
- // if yes, add first person to lift
- // up to maxCapacity
- // if there are still more people, add next person IF maxWeight is not met
- // if lift is empty go back to ground floor
- // repeat
- var Persons = [{ weight: 50, target: 2 }, { weight: 80, target: 3 }, { weight: 40, target: 5 }, { weight: 120, target: 1 }];
- var Lift = (function () {
- function Lift() {
- _classCallCheck(this, Lift);
- this.maxWeight = 200;
- this.maxCapacity = 2;
- this.currentCapacity = 0;
- this.currentWeight = 0;
- this.totalMoves = 0;
- this.currentFloor = 0;
- this.peopleInTheLift = [];
- }
- _createClass(Lift, [{
- key: "onBoardPerson",
- value: function onBoardPerson(person) {
- if (person.weight + this.currentWeight <= this.maxWeight) {
- this.peopleInTheLift.push(person);
- this.currentCapacity += 1;
- this.currentWeight += person.weight;
- }
- }
- }, {
- key: "dropOffPeople",
- value: function dropOffPeople() {
- var _this = this;
- this.peopleInTheLift.forEach(function (person) {
- if (_this.currentFloor !== person.target) {
- _this.totalMoves++;
- _this.currentFloor = person.target;
- _this.currentWeight = _this.currentWeight - person.weight;
- _this.currentCapacity = _this.currentCapacity - 1;
- _this.peopleInTheLift = _this.peopleInTheLift.slice(1, _this.peopleInTheLift.length);
- } else {
- _this.currentWeight = _this.currentWeight - person.weight;
- _this.currentCapacity = _this.currentCapacity - 1;
- _this.peopleInTheLift = _this.peopleInTheLift.slice(1, _this.peopleInTheLift.length);
- }
- });
- this.currentFloor = 0;
- this.totalMoves++;
- }
- }, {
- key: "countMove",
- value: function countMove() {
- return this.totalMoves;
- }
- }]);
- return Lift;
- })();
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">Elevator:
- // There is an elevator in a building with M floors.
- // This elevator can take a max of X people at a time or max of total weight Y.
- // Given that a set of people and their weight and the floor they need to stop,
- // how many stops has the elevator taken to serve all the people?
- //Consider the elevator serves in "first come first serve" basis and the order for the queue can not be changed.
- //
- // Example:
- // Let Array A be the weight of each person A = [60, 80, 40].
- // Let Array B be the floors where each person needs to be dropped off B = [2, 3, 5].
- //
- // The building has 5 floors, maximum of 2 persons are allowed in the elevator
- // at a time with max weight capacity being 200. For this example, the elevator would take total
- // of 5 stops in floors: 2, 3, G, 5 and finally G.
- "use strict";
- const A = [50, 80, 40, 120];
- const B = [2, 3, 5, 1];
- const maxWeight = 200;
- const maxCapacity = 2;
- let journeys;
- // are there more people
- // if yes, add first person to lift
- // up to maxCapacity
- // if there are still more people, add next person IF maxWeight is not met
- // if lift is empty go back to ground floor
- // repeat
- const Persons = [
- {weight: 50, target: 2},
- {weight: 80, target: 3},
- {weight: 40, target: 5},
- {weight: 120, target: 1},
- ];
- class Lift {
- constructor() {
- this.maxWeight = 200;
- this.maxCapacity = 2;
- this.currentCapacity = 0;
- this.currentWeight = 0;
- this.totalMoves = 0;
- this.currentFloor = 0;
- this.peopleInTheLift = [];
- }
- onBoardPerson(person) {
- if ((person.weight + this.currentWeight) <= this.maxWeight ) {
- this.peopleInTheLift.push(person);
- this.currentCapacity += 1;
- this.currentWeight += person.weight;
- }
- }
- dropOffPeople() {
- this.peopleInTheLift.forEach(person => {
- if (this.currentFloor !== person.target) {
- this.totalMoves ++;
- this.currentFloor = person.target;
- this.currentWeight = this.currentWeight - person.weight;
- this.currentCapacity = this.currentCapacity - 1;
- this.peopleInTheLift = this.peopleInTheLift.slice(1, this.peopleInTheLift.length);
- } else {
- this.currentWeight = this.currentWeight - person.weight;
- this.currentCapacity = this.currentCapacity - 1;
- this.peopleInTheLift = this.peopleInTheLift.slice(1, this.peopleInTheLift.length);
- }
- });
- this.currentFloor = 0;
- this.totalMoves ++;
- }
- countMove() {
- return this.totalMoves;
- }
- }
- </script></body>
- </html>
Add Comment
Please, Sign In to add comment