Data Structures & AlgorithmsModerateGATE CS · Product-company placement
A 4 × 4 grid has rows 6 6 6 4 / 7 6 5 2 / 2 9 8 2 / 5 1 8 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)?
- A. 36
- B. 53
- C. 29
- D. 35
Correct answer: D. 35
dp[i][j] = grid[i][j] + min(dp[i−1][j], dp[i][j−1]) gives 35. Greedily stepping to the smaller neighbour gives 36, 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
- Bellman–Ford runs from A on the directed graph whose edges are relaxed in EVERY pass in this fixed order: F→A (9), D→A (4), E→D (8), C→B…
- Floyd–Warshall runs on vertices 1–4 with directed edges 1→2 (1), 1→3 (5), 2→1 (1), 2→4 (8), 3→1 (3), 3→2 (9), 4→3 (2). After the…
- A tree on vertices 1–9 has edges 2–1, 3–1, 4–3, 5–2, 6–4, 7–6, 8–6, 9–8. What are its diameter and radius, and which vertex/vertices form…
- Five matrices have dimensions 12×10 , 10×4 , 4×6 , 6×6 , 6×8. What is the minimum number of scalar multiplications needed to compute their…
- An undirected graph has edges A–D, A–F, A–G, B–C, B–F, C–D, C–E, E–G, F–G. An ITERATIVE DFS starts with stack [A]; it pops a vertex, skips…
- What is the length of the longest common subsequence of ONTOLOGY and TOPOLOGY, and how many DISTINCT longest common subsequences (as…