Data Structures & AlgorithmsModerateProduct-company placement

A 4 × 4 grid has rows 3 6 2 5 / 6 9 2 3 / 8 7 2 2 / 4 2 4 9. 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. 36
  2. B. 40
  3. C. 23
  4. D. 26

Correct answer: D. 26

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic