Minimum average waiting time Hackerrank

The challenge link can be found here

Problem Statement

Tieu owns a pizza restaurant and he runs it in his own way. Whereas in a typical restaurant the customer is served first and foremost, the first rule is Tieu simply minimizes the average waiting time of its customers. Therefore, he decides who serves first, no matter how sooner or later the person arrives.

Different types of pizza take different amounts of time to prepare. Also, once he starts making pizza, he cannot make another pizza until the first pizza is fully cooked. Let's say we have three customers who arrive at time t = 0, t = 1 and t = 2, respectively, and the time it takes to prepare their pizza is 3, 9 and 6, respectively. If Tieu applies the first come, first served rule, then the waiting times of three customers are 3, 11 and 16 respectively. The average waiting time in this case is (3 + 11 + 16) / 3 = 10. This is not an optimized solution. After serving the first customer at time t = 3, Tieu can choose to serve the third customer. In this case, the waiting time will be 3, 7 and 17, respectively. Therefore, the average waiting time is (3 + 7 + 17) / 3 = 9.

Help Tieu achieve the lowest average waiting time. In the name of simplicity, just find a whole fraction of the minimum average waiting time.

Input format

The first line contains an integer N, which is the number of clients. In the next N lines, the i-th line contains two spaces separated by numbers Ti and Li. Ti is the time the customer ordering a pizza and Lee is the time it takes to make that pizza. Output format

Displays the integer part of the minimum average latency.

Limitations

1 ≤ N ≤ 10 ^ 5

0 ≤ Ti ≤ 10 ^ 9

1 ≤ Li ≤ 10 ^ 9

Note

The waiting time is calculated as the difference between the time a customer orders the pizza (the time they enter the store) and the time she serves.

Cook is unaware of future orders.

I've been doing this for several hours.

I'm sure my problems are related to how I increase the overall wait time.

Any help would be much appreciated.

code:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;

public class Solution {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();

        MinimumAverageWaitingTime mawt = new MinimumAverageWaitingTime();

        while(n-- > 0) mawt.insert(s.nextLong(), s.nextLong());
        System.out.print(mawt.calculateAverageWaitingTime());
    }
}

class MinimumAverageWaitingTime {
    private PriorityQueue<e_time_p_time> incomingOrders = new PriorityQueue<>(10, new Comparator<e_time_p_time>(){
        //Order by the customerWaitTime ASC
        @Override public int compare(e_time_p_time w, e_time_p_time w1) {
            return (int) (w.entryTime - w1.entryTime);
        }
    });
    private PriorityQueue<e_time_p_time> awaitingOrders = new PriorityQueue<>(10, new Comparator<e_time_p_time>(){
        //Order by the difference between entrytime and pizzaCookTime ASC
        @Override public int compare(e_time_p_time w, e_time_p_time w1) {
            return (int) (Math.abs(w.entryTime - w.pizzaCookTime) - Math.abs(w1.entryTime - w1.pizzaCookTime));
        }
    });

    private long total = 0l;

    public void insert(long customerWaitTime, long pizzaCookTime) {                
        incomingOrders.add(new e_time_p_time(customerWaitTime, pizzaCookTime));
    }

    public long calculateAverageWaitingTime() {
        int size = incomingOrders.size();

        e_time_p_time currentOrder = null;
        e_time_p_time laterOrders = null;

        while(incomingOrders.size() > 0) {
            //Start by getting the customer that has the earliest arrival time (the queue is sorted that way)
            currentOrder = incomingOrders.remove();

            //Calculate it waiting time. 
            total += currentOrder.entryTime + currentOrder.pizzaCookTime;

            do {
                /*Move all the customers that entered the shop while the current pizza is in the oven
                  to the awaitingOrders orders queue*/
                laterOrders = incomingOrders.remove(); 
                awaitingOrders.add(laterOrders);
            } while (currentOrder.pizzaCookTime >= laterOrders.entryTime && incomingOrders.size() > 0);

            //Go through awaitingOrders queue and calculate waiting time for the remaining orders
            //(The queue is sorted as the difference between entrytime and pizzaCookTime ASC)
            while(awaitingOrders.size() > 0) {
                e_time_p_time shortestOrder = awaitingOrders.remove();
                long waitTimeBeforeCooking = Math.abs((shortestOrder.entryTime + shortestOrder.pizzaCookTime) - currentOrder.entryTime);
                total += waitTimeBeforeCooking;
            }
        }

        //It supposed to be the average time, but first I need the total to be correct, and right now, it not...
        System.out.println("\nTotal waiting time: ");
        return total;
    }

    private static class e_time_p_time {
        private long entryTime;
        private long pizzaCookTime;

        e_time_p_time(long entryTime, long pizzaCookTime) {
            this.entryTime = entryTime;
            this.pizzaCookTime = pizzaCookTime;
        }

    }
}

      

+3


source to share


1 answer


In this code:

     do {
            /*Move all the customers that entered the shop while the current pizza is in the oven
              to the awaitingOrders orders queue*/
            laterOrders = incomingOrders.remove(); 
            awaitingOrders.add(laterOrders);
        } while (currentOrder.pizzaCookTime >= laterOrders.entryTime && incomingOrders.size() > 0);

      



Several things are wrong here:

  • You always add at least one item pending Orders - but what if no one enters the store while the current pizza is in the oven? (for example, for the last pizza)
  • You are comparing pizzaCookTime - like ten minutes, with enterTime, for example. 4 pm. This doesn't seem right - shouldn't you be comparing the time it takes for the pizza to complete with entryTime?
+2


source







All Articles