Data Structures & AlgorithmsModerateGATE CS · Product-company placement

A 4 × 4 grid has rows 1 5 2 1 / 1 1 5 4 / 7 4 6 2 / 1 6 3 8. Moving only right or down from the top-left to the bottom-right cell, what is the minimum possible sum of the cells on the path (both ends included)?

  1. A. 24
  2. B. 22
  3. C. 30
  4. D. 21

Correct answer: B. 22

dp[i][j] = grid[i][j] + min(dp[i−1][j], dp[i][j−1]) gives 22. Greedily stepping to the smaller neighbour gives 24, because a cheap step can lead into an expensive region.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic