Advertisement
hakemon

elevator_cd

Oct 21st, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var elevator = {
  2.     mp: 0,
  3.     mw: 0,
  4.     pc: 0,
  5.     w: 0,
  6.     sc: 0,
  7.     fc: 0,
  8.     fl: [],
  9.     push: function(person) {
  10.         if (this.w + person.weight <= this.mw && this.pc +1 <= this.mp) {
  11.             this.w += person.weight;
  12.             this.pc += 1;
  13.             if (this.fl.indexOf(person.floor) === -1) {
  14.                 this.fl.push(person.floor);
  15.             }
  16.             return true;
  17.         }
  18.         return false;
  19.     },
  20.     clear: function() {
  21.         this.fc += (this.fl.length + 1);
  22.         this.pc = 0;
  23.         this.w = 0;
  24.         this.fl = [];
  25.     },
  26.     init: function(mp, mw) {
  27.         this.mw = mw;
  28.         this.mp = mp;
  29.     }
  30. }
  31.  
  32.  
  33. function solution(A, B, M, X, Y) {
  34.     elevator.init(X, Y);
  35.     var people = A.map(function(c, i) {
  36.         return {weight: c, floor:B[i]};
  37.     });
  38.     while (people.length >0) {
  39.         while (people.length >0 && elevator.push(people[0])) {
  40.             people.shift();
  41.         }
  42.         elevator.clear();
  43.     }
  44.     return elevator.fc;
  45. }
  46.  
  47. var A = [60, 80, 40];
  48. var B = [2, 3, 5];
  49. var M = 5;
  50. var X = 2;
  51. var Y = 200;
  52. var x = solution(A, B, M, X, Y);
  53. console.log(x);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement