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)?
- A. 24
- B. 22
- C. 30
- 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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- How many calls of g (including the first) are made when g(7) is evaluated, and what value is returned? c int g(int n) { if (n <= 2) return…
- The words trie, tried, trip, try, tree are inserted into an empty trie (one node per character, shared prefixes share nodes, plus a root)…
- In the grid 21112 / 11110 / 10111 / 11111 (rows top to bottom; 2 = rotten, 1 = fresh, 0 = empty), every minute each rotten orange rots its…
- An undirected graph has edges A–E, A–H, B–F, C–E, C–G, D–G, D–H, E–F, E–G, F–H. BFS starts at A and always scans neighbours in…
- A tree on vertices 1–10 has edges 2–1, 3–2, 4–1, 5–4, 6–1, 7–3, 8–7, 9–5, 10–9. What is the size of a minimum DOMINATING set (every vertex…
- What is the length of the longest common subsequence of CONTEST and RECESSION, and how many DISTINCT longest common subsequences (as…