Approach
Breadth-first search
For Implement Router, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 106 lines of Java from the credited upstream file 3508.java.
- The implementation visibly relies on sequence storage, hash lookup, ordered lookup, work queue, cached states.
- 2 loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Packet implements Comparable<Packet> {2 public int source;3 public int destination;4 public int timestamp;5 6 public Packet(int source, int destination, int timestamp) {7 this.source = source;8 this.destination = destination;9 this.timestamp = timestamp;10 }11 12 @Override13 public int compareTo(Packet other) {14 if (source != other.source)15 return Integer.compare(source, other.source);16 if (destination != other.destination)17 return Integer.compare(destination, other.destination);18 return Integer.compare(timestamp, other.timestamp);19 }20 21 @Override22 public boolean equals(Object o) {23 if (this == o)24 return true;25 if (o == null || getClass() != o.getClass())26 return false;27 Packet packet = (Packet) o;28 return source == packet.source && destination == packet.destination &&29 timestamp == packet.timestamp;30 }31 32 @Override33 public int hashCode() {34 return Objects.hash(source, destination, timestamp);35 }36}37 38class Router {39 public Router(int memoryLimit) {40 this.memoryLimit = memoryLimit;41 }42 43 public boolean addPacket(int source, int destination, int timestamp) {44 Packet packet = new Packet(source, destination, timestamp);45 if (uniquePackets.contains(packet))46 return false;47 if (packetQueue.size() == memoryLimit)48 forwardPacket();49 packetQueue.add(packet);50 uniquePackets.add(packet);51 destinationTimestamps.computeIfAbsent(destination, k -> new ArrayList<>()).add(timestamp);52 return true;53 }54 55 public List<Integer> forwardPacket() {56 if (packetQueue.isEmpty())57 return Collections.emptyList();58 Packet nextPacket = packetQueue.poll();59 uniquePackets.remove(nextPacket);60 processedPacketIndex.merge(nextPacket.destination, 1, Integer::sum);61 return Arrays.asList(nextPacket.source, nextPacket.destination, nextPacket.timestamp);62 }63 64 public int getCount(int destination, int startTime, int endTime) {65 if (!destinationTimestamps.containsKey(destination))66 return 0;67 List<Integer> timestamps = destinationTimestamps.get(destination);68 final int startIndex = processedPacketIndex.getOrDefault(destination, 0);69 final int lowerBoundIndex = firstGreaterEqual(timestamps, startIndex, startTime);70 final int upperBoundIndex = firstGreater(timestamps, lowerBoundIndex, endTime);71 return upperBoundIndex - lowerBoundIndex;72 }73 74 private final int memoryLimit;75 private final TreeSet<Packet> uniquePackets = new TreeSet<>();76 private final Queue<Packet> packetQueue = new LinkedList<>();77 private final Map<Integer, List<Integer>> destinationTimestamps = new HashMap<>();78 private final Map<Integer, Integer> processedPacketIndex = new HashMap<>();79 80 private int firstGreaterEqual(List<Integer> timestamps, int startIndex, int startTime) {81 int l = startIndex;82 int r = timestamps.size();83 while (l < r) {84 final int m = (l + r) / 2;85 if (timestamps.get(m) >= startTime)86 r = m;87 else88 l = m + 1;89 }90 return l;91 }92 93 private int firstGreater(List<Integer> timestamps, int startIndex, int endTime) {94 int l = startIndex;95 int r = timestamps.size();96 while (l < r) {97 final int m = (l + r) / 2;98 if (timestamps.get(m) > endTime)99 r = m;100 else101 l = m + 1;102 }103 return l;104 }105}106