Advertisement
hakemon

elevator.java

Oct 21st, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. class Solution {
  2.     public int solution(int[] A, int[] B, int M, int X, int Y) {
  3.         LinkedList<Person> q = new LinkedList<>();
  4.         for (int i = 0; i < A.length; i++) {
  5.             q.add(new Person(A[i], B[i]));
  6.         }
  7.         int counter = 0;
  8.         Elevator elevator = new Elevator(X, Y);
  9.         do {
  10.            
  11.             if (elevator.addPerson(q.peek()))
  12.                 q.remove();
  13.             else {
  14.                 counter += elevator.runOneRound();
  15.                 elevator = new Elevator(X, Y);
  16.             }
  17.         } while (!q.isEmpty());
  18.         counter += elevator.runOneRound();
  19.         return counter;
  20.        
  21.     }
  22.     static class Elevator {
  23.         private int peopleCapacity;
  24.         private int weightCapacity;
  25.         private int currentPeople;
  26.         private int currentWeight;
  27.         private Set<Integer> storeys;
  28.         public Elevator(int peopleCapacity, int weightCapacity) {
  29.             this.peopleCapacity = peopleCapacity;
  30.             this.weightCapacity = weightCapacity;
  31.             this.storeys = new HashSet<>();
  32.         }
  33.         public boolean addPerson(Person person) {
  34.             boolean added = false;
  35.             if (this.currentPeople + 1 <= this.peopleCapacity && this.currentWeight + person.getWeight() <= this.weightCapacity) {
  36.                 this.currentPeople++;
  37.                 this.currentWeight += person.getWeight();
  38.                 this.storeys.add(person.getTargetFloor());
  39.                 added = true;
  40.             }
  41.             return added;
  42.         }
  43.         public int runOneRound() {
  44.             return this.storeys.size() + 1;
  45.         }
  46.     }
  47.    
  48.     static class Person {
  49.         private int weight;
  50.         private int targetFloor;
  51.         public Person(int weight, int targetFloor) {
  52.             this.weight = weight;
  53.             this.targetFloor = targetFloor;
  54.         }
  55.         public int getWeight() {
  56.             return this.weight;
  57.         }
  58.         public int getTargetFloor() {
  59.             return this.targetFloor;
  60.         }
  61.     }
  62.    
  63.     public static void main(String[] args) {
  64.         int[] weights = {60, 80, 40};
  65.         int[] floors = {3, 2, 4};
  66.         Solution s = new Solution();
  67.         System.out.println(s.solution(weights, floors, 5, 2, 200));
  68.     }
  69.    
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement