Google Interview Question
Software Engineer / DevelopersCountry: India
1: lets assume that the distance between each pole is the same or contant.
2: lets assume that the minimum length of the wire required to join the two poles is the constant distance of the pole between each other.
3: less compare the two poles if the height are the same then we know that we need just the constant distance of the pole as the required length of wire to connect the poles. If the height of the two poles are not the same then we substract the poles among them between the highest and the small pole after substracting, we add the constant distance between two poles and the substracted value, so thats going to be our required wire length to join the two not poles with wire.
Thinking this approach will work.
This is exactly what I think too,
For e.g let's say the maximum pole height is 10 units and the constant distance between two poles is 5 units, therefore the length of wire to connect two poles of the same height is 5 units, but the length of wire required to connect the pole A(10 units) and pole B(6 units) is 5 + (10 - 6) which will give 9 units length of wire required to connect the two poles A and B.
Therefore the solution is to get the length of wire between the current pole and the next pole by finding their height difference and adding it to the constant distance between the two poles and then adding the length to the total length of wire required.
Let's assume
- Md Omar Faroque August 24, 20221. The distance between two poles is 1 unit.
2. The tallest pole is 10unit height.
3. The shortest one is 1 unit, clear from the problem.
Now,
If two tallest pole are neighbor (10 10), then you need only 1 unit of wire to connect.
But if one pole is 10 and another pole is 1, now you need a diagonal distance (sqrt(10*10+1))
Happy case: All the pole is same height
Worst case: One pole is max height, 10 and the next one is 1. It will look like a triangular wave.
Just need to sum up all the non horizontal(diagonal) line of the wave.