diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/NetworkPacket.java | 83 |
1 files changed, 55 insertions, 28 deletions
diff --git a/src/main/NetworkPacket.java b/src/main/NetworkPacket.java index 805dcf5..7aeea88 100644 --- a/src/main/NetworkPacket.java +++ b/src/main/NetworkPacket.java @@ -1,49 +1,76 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Deque; +import java.util.LinkedList; import java.util.Scanner; public class NetworkPacket { - public static class Request { - public int arrivalTime; - public int processTime; + static class Request { + int arrivalTime; + int processTime; - public Request(int arrivalTime, int processTime) { + Request(int arrivalTime, int processTime) { this.arrivalTime = arrivalTime; this.processTime = processTime; } } - public static class Response { - public boolean dropped; - public int startTime; + static class Response { + boolean dropped; + int startTime; - public Response(boolean dropped, int startTime) { + Response(boolean dropped, int startTime) { this.dropped = dropped; this.startTime = startTime; } } - public static class Buffer { - private int size; - private ArrayList<Integer> finishTime = new ArrayList<Integer>(); + static class Buffer { + private int capacity; + private Deque<Integer> finishTime; - public Buffer(int size) { - this.size = size; + // My addition to the starter file + private int runningTimeCounter = 0; + // End of my addition to the starter file + + Buffer(int capacity) { + this.capacity = capacity; + this.finishTime = new LinkedList<>(); } - public Response processRequests(Request request) { - // write your code here - if (this.size == 0) - return new Response(false,-1); - else { + + Response processRequest(Request request) { + if (runningTimeCounter < request.arrivalTime) if (finishTime.size() == 0) + runningTimeCounter = request.arrivalTime; + else if (finishTime.getLast() < request.arrivalTime) + runningTimeCounter = request.arrivalTime; + + while (finishTime.size() > 0) { + int j = finishTime.getFirst(); + if (j <= request.arrivalTime) + finishTime.pop(); + else + break; } + + if (finishTime.size() >= capacity) { + return new Response(true, -1); + } + Response response = new Response(false, runningTimeCounter); + runningTimeCounter = runningTimeCounter + request.processTime; + if (finishTime.size() > 0) + finishTime.addLast(finishTime.getLast() + request.processTime); + else + finishTime.addLast(runningTimeCounter); + return response; } } - private static ArrayList<Request> ReadQueries(Scanner scanner) throws IOException { + + private static ArrayList<Request> ReadQueries(Scanner scanner) { int requests_count = scanner.nextInt(); - ArrayList<Request> requests = new ArrayList<Request>(); + ArrayList<Request> requests = new ArrayList<>(); for (int i = 0; i < requests_count; ++i) { int arrival_time = scanner.nextInt(); int process_time = scanner.nextInt(); @@ -52,16 +79,16 @@ public class NetworkPacket { return requests; } - public static ArrayList<Response> processRequests(ArrayList<Request> requests, Buffer buf) { + static ArrayList<Response> processRequests(ArrayList<Request> requests, Buffer buf) { ArrayList<Response> responses = new ArrayList<>(); - for (int i = 0; i < requests.size(); i++) { - responses.add(buf.processRequests(requests.get(i))); + for (Request request : requests) { + responses.add(buf.processRequest(request)); } return responses; } + private static void PrintResponses(ArrayList<Response> responses) { - for (int i = 0; i < responses.size(); ++i) { - Response response = responses.get(i); + for (Response response : responses) { if (response.dropped) { System.out.println(-1); } else { @@ -70,11 +97,11 @@ public class NetworkPacket { } } - public static void main(String[] args) throws IOException { + public static void main(String[] args) { Scanner scanner = new Scanner(System.in); - int buffer_max_size = scanner.nextInt(); - Buffer buffer = new Buffer(buffer_max_size); + int bufferMaxSize = scanner.nextInt(); + Buffer buffer = new Buffer(bufferMaxSize); ArrayList<Request> requests = ReadQueries(scanner); ArrayList<Response> responses = processRequests(requests, buffer); |