Feature #3: Minimum Hops

Implementing the "Minimum Hops" feature for our "Network" project.

Description

We have created a linear network topology by placing routers in a straight chain. Each router has a specific TTL (Time To Live) value associated with it. Hence, a router is able to send a packet to a downstream neighbor at most h[i] hops away (you may think of this as one to the right). Here, h[i] denotes the TTL value of the ith router. A packet has arrived at the first router, h[0], and it needs to be sent to the last router in the chain. You want to get the packet to the destination in the fewest possible transmissions, which means using the fewest possible intermediate routers.

We’ll be provided with a list containing the TTL values of each router. The router’s position in the chain will be denoted by list indexes. For example, the TTL of the first router in the chain is at index 0 of the input array, the second router is at index 1, and so on. We have to compute the minimum number of hops to transmit the packet from the initial to the final router.

Let’s try to understand this better with an illustration:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.