Class Plumber

java.lang.Object
topics.greedy.plumber.Plumber

public class Plumber extends Object

Single-Plumber Scheduling

This class models the classic scheduling problem where a single worker handles a sequence of tasks with known durations. The order of execution strictly determines the total accumulated customer waiting time.

The Greedy Strategy

To minimize the total waiting time for all customers, the optimal greedy choice is the Shortest Processing Time (SPT) first rule. By sorting the tasks in ascending order of their durations, we delay the fewest number of subsequent tasks.

Author:
vicegd
  • Constructor Details

    • Plumber

      public Plumber(int[] tasks)
      Builds a plumber instance with task durations.
      Parameters:
      tasks - Duration of each task in the provided execution order.
      Throws:
      NullPointerException - if tasks is null.
      IllegalArgumentException - if any task duration is negative.
  • Method Details

    • getTotalTimeOfWait

      public int getTotalTimeOfWait()
      Calculates total waiting time for the current task order.
      Example for durations [2, 5, 4]: Waiting times are 2, (2+5=7), (7+4=11). Total = 2 + 7 + 11 = 20.
      Returns:
      Total waiting time for the current order.
      Throws:
      ArithmeticException - if the accumulated value exceeds integer bounds.
    • getOptimalTotalTimeOfWait

      public int getOptimalTotalTimeOfWait()
      Calculates minimum possible total waiting time (The Greedy Optimum).
      Sorts task durations ascending, then computes the accumulated waiting time.
      Returns:
      Minimal total waiting time among all possible task permutations.