Class Plumber
java.lang.Object
topics.greedy.Plumber
Single-plumber scheduling utility.
This class models the classic scheduling problem where one plumber handles
a sequence of tasks with known durations. The order of tasks determines the
total customer waiting time.
For a fixed order, the waiting-time sum is computed in O(n). For an
optimal order (Shortest Processing Time first), use then
getOptimalTotalTimeOfWait().- Author:
- vicegd
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintCalculates minimum possible total waiting time (greedy optimal).intCalculates total waiting time for the current task order.
-
Constructor Details
-
Plumber
public Plumber(int[] tasks) Builds a plumber instance with task durations.- Parameters:
tasks- duration of each task in the execution order- Throws:
NullPointerException- iftasksis nullIllegalArgumentException- 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, 7, 11, total = 20.- Returns:
- total waiting time for the current order
- Throws:
ArithmeticException- if the accumulated value exceedsint
-
getOptimalTotalTimeOfWait
public int getOptimalTotalTimeOfWait()Calculates minimum possible total waiting time (greedy optimal). The optimal strategy is Shortest Processing Time first (SPT): sort task durations ascending, then compute waiting time.- Returns:
- minimal total waiting time among all possible task orders
-