Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int solution(int[] A, int[] B, int M, int X, int Y) {
- LinkedList<Person> q = new LinkedList<>();
- for (int i = 0; i < A.length; i++) {
- q.add(new Person(A[i], B[i]));
- }
- int counter = 0;
- Elevator elevator = new Elevator(X, Y);
- do {
- if (elevator.addPerson(q.peek()))
- q.remove();
- else {
- counter += elevator.runOneRound();
- elevator = new Elevator(X, Y);
- }
- } while (!q.isEmpty());
- counter += elevator.runOneRound();
- return counter;
- }
- static class Elevator {
- private int peopleCapacity;
- private int weightCapacity;
- private int currentPeople;
- private int currentWeight;
- private Set<Integer> storeys;
- public Elevator(int peopleCapacity, int weightCapacity) {
- this.peopleCapacity = peopleCapacity;
- this.weightCapacity = weightCapacity;
- this.storeys = new HashSet<>();
- }
- public boolean addPerson(Person person) {
- boolean added = false;
- if (this.currentPeople + 1 <= this.peopleCapacity && this.currentWeight + person.getWeight() <= this.weightCapacity) {
- this.currentPeople++;
- this.currentWeight += person.getWeight();
- this.storeys.add(person.getTargetFloor());
- added = true;
- }
- return added;
- }
- public int runOneRound() {
- return this.storeys.size() + 1;
- }
- }
- static class Person {
- private int weight;
- private int targetFloor;
- public Person(int weight, int targetFloor) {
- this.weight = weight;
- this.targetFloor = targetFloor;
- }
- public int getWeight() {
- return this.weight;
- }
- public int getTargetFloor() {
- return this.targetFloor;
- }
- }
- public static void main(String[] args) {
- int[] weights = {60, 80, 40};
- int[] floors = {3, 2, 4};
- Solution s = new Solution();
- System.out.println(s.solution(weights, floors, 5, 2, 200));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement