1

I came across about this problem in a interview, how can we solve this in O(n):

Given a grid of m*n, a player P1 & P2 is at node (x1,y1) & (x2,y2) respectively. There are n gems placed on different positions of grid i.e. (G1,G2,G3…..Gn). Calculate the minimum cost required to pick all the gems. A gem can be picked by a single player. Gems should be picked in particular order i.e. G1 should be picked first then G2 & then G3 so on.

For example:

P1 (1,1) & P2(3,4)

There are 4 gems:

G1 (1,1), G2(2,2),G3(3,2),G4(4,2)

Total cost: 5 P1 will pick G4. P2 will pick G1,G2, G3. Complexity : O(n)

smartsn123
  • 115
  • 9

0 Answers0