Filip_Markoski

[ADSx] - PetrolPumps Queue

Jan 18th, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.28 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4.  
  5. /**
  6.  * https://www.geeksforgeeks.org/find-a-tour-that-visits-all-stations/
  7.  * <p>
  8.  * Suppose there is a circle. There are n petrol pumps on that circle. You are given two sets of data.
  9.  * <p>
  10.  * 1. The amount of petrol that every petrol pump has.
  11.  * 2. Distance from that petrol pump to the next petrol pump.
  12.  * <p>
  13.  * Calculate the first point from where a truck will be able to complete the circle
  14.  * (The truck will stop at each petrol pump and it has infinite capacity).
  15.  * Expected time complexity is O(n). Assume for 1 litre petrol, the truck can go 1 unit of distance.
  16.  * <p>
  17.  * For example, let there be 4 petrol pumps with amount of petrol and distance to next petrol pump value pairs as {4, 6}, {6, 5}, {7, 3} and {4, 5}. The first point from where truck can make a circular tour is 2nd petrol pump. Output should be “start = 1” (index of 2nd petrol pump).
  18.  */
  19.  
  20. class PetrolPump {
  21.     int petrol;
  22.     int distance;
  23.  
  24.     public PetrolPump(int petrol, int distance) {
  25.         this.petrol = petrol;
  26.         this.distance = distance;
  27.     }
  28.  
  29.     public int getPetrol() {
  30.         return petrol;
  31.     }
  32.  
  33.     public int getDistance() {
  34.         return distance;
  35.     }
  36.  
  37.     @Override
  38.     public String toString() {
  39.         return String.format("<P:%d, D:%d>", petrol, distance);
  40.     }
  41. }
  42.  
  43. class Node {
  44.  
  45.     int position;
  46.     int petrol;
  47.     int distance;
  48.     Node next;
  49.  
  50.     public Node(int pos, int a, int b) {
  51.         position = pos;
  52.         petrol = a;
  53.         distance = b;
  54.         next = null;
  55.     }
  56. }
  57.  
  58. public class PetrolPumpsQueue {
  59.  
  60.     public static int solveQueue(int length, Queue<PetrolPump> queue) {
  61.  
  62.         for (int i = 0; i < length; i++) {
  63.  
  64.             int petrolSum = 0;
  65.             boolean valid = true;
  66.  
  67.             for (int j = 0; j < length; j++) {
  68.  
  69.                 PetrolPump current = queue.poll();
  70.                 queue.add(current);
  71.  
  72.                 petrolSum += current.getPetrol();
  73.  
  74.                 if (petrolSum < current.getDistance()) valid = false;
  75.                 else petrolSum -= current.getDistance();
  76.  
  77.                 /*System.out.printf("petrolSum: %d, distance: %d, valid: %b \n",
  78.                         petrolSum, current.getDistance(), valid);
  79.                 System.out.println(queue);*/
  80.             }
  81.  
  82.             if (valid) return i;
  83.  
  84.             PetrolPump poll = queue.poll();
  85.             queue.add(poll);
  86.         }
  87.  
  88.         return -1;
  89.     }
  90.  
  91.     public static int solveArray(PetrolPump array[], int n) {
  92.         /**
  93.          * https://www.geeksforgeeks.org/find-a-tour-that-visits-all-stations/
  94.          * */
  95.  
  96.         int start = 0;
  97.         int end = 1;
  98.         int curr_petrol = array[start].petrol - array[start].distance;
  99.  
  100.         // If current amount of petrol in truck becomes less than 0, then
  101.         // remove the starting petrol pump from tour
  102.         while (end != start || curr_petrol < 0) {
  103.  
  104.             // If current amount of petrol in truck becomes less than 0, then
  105.             // remove the starting petrol pump from tour
  106.             while (curr_petrol < 0 && start != end) {
  107.                 // Remove starting petrol pump. Change start
  108.                 curr_petrol -= array[start].petrol - array[start].distance;
  109.                 start = (start + 1) % n;
  110.  
  111.                 // If 0 is being considered as start again, then there is no
  112.                 // possible solution
  113.                 if (start == 0)
  114.                     return -1;
  115.             }
  116.             // Add a petrol pump to current tour
  117.             curr_petrol += array[end].petrol - array[end].distance;
  118.  
  119.             end = (end + 1) % n;
  120.         }
  121.  
  122.         // Return starting point
  123.         return start;
  124.     }
  125.  
  126.     public static int solveSLL(Node head) {
  127.         /**
  128.          * https://ideone.com/Muk97k
  129.          * */
  130.        
  131.         Node start = head;
  132.         do {
  133.             Node curr = start;            // Storing the starting node
  134.             int petrol_left = 0;
  135.  
  136.             // if petrol is left and the current node is not the starting node
  137.             do {
  138.                 petrol_left += curr.petrol - curr.distance;
  139.                 curr = curr.next;
  140.             }
  141.             while (petrol_left >= 0 && curr != start);
  142.  
  143.             if (petrol_left >= 0)
  144.                 return start.position;
  145.  
  146.             start = start.next;
  147.  
  148.         } while (start != head);
  149.  
  150.         return -1;
  151.     }
  152.  
  153.     public static void main(String args[]) {
  154.         PetrolPump array[] = {
  155.                 new PetrolPump(6, 4),
  156.                 new PetrolPump(3, 6),
  157.                 new PetrolPump(7, 3)
  158.         };
  159.  
  160.         int result;
  161.  
  162.         result = solveArray(array, array.length);
  163.         System.out.println("Array: " + result);
  164.  
  165.         Queue<PetrolPump> queue = new LinkedList<>();
  166.         queue.addAll(Arrays.asList(array));
  167.  
  168.         result = solveQueue(queue.size(), queue);
  169.         System.out.println("Queue: " + result);
  170.  
  171.         Node head = new Node(1, 4, 6);
  172.         head.next = new Node(2, 6, 5);
  173.         head.next.next = new Node(3, 7, 3);
  174.         head.next.next.next = new Node(4, 4, 5);
  175.         head.next.next.next.next = head;
  176.  
  177.         result = solveSLL(head);
  178.         System.out.println("SLL: " + result);
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment