Data Structures & AlgorithmsHardInfosys · Accenture pattern
Houses in a row hold [3, 2, 7, 10, 1, 9]. No two ADJACENT houses may both be chosen. What is the maximum total? (dp[i] = max(dp[i − 1], dp[i − 2] + a[i]).)
- A. 19
- B. 20
- C. 21
- D. 22
Correct answer: D. 22
dp: 3, 3, 10, 13, 13, 22. Best set: 3 + 10 + 9 = 22 (houses 1, 4, 6). Taking every other house from the start (3 + 7 + 1 = 11) is far worse, and 3 + 7 + 9 = 19 misses that choosing 10 (and giving up its neighbour 7) pays more.
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
- The set [3, 9, 7, 3, 11] is split into two subsets so that the absolute difference of their sums is as small as possible (subset-sum DP…
- Digits map to letters A = 1 … Z = 26. In how many ways can the string '2263' be decoded? (dp over prefixes; a pair counts only if it is…
- Keys 50, 30, 20, 10, 40, 70, 60 are inserted into an empty BST. An iterative inorder traversal pushes nodes while walking left and pops to…
- A course plan has prerequisites (a → b means a must come before b): 1→3, 2→3, 3→5, 4→5, 5→6, 2→4, 6→7. Any number of courses with all…
- Flights (directed, cost): 0→1 100, 1→2 100, 2→3 100, 0→2 500, 1→3 600, 0→3 900. What is the cheapest cost from 0 to 3 using AT MOST 1…
- What is the diameter (largest distance between any two nodes) of the weighted tree with edges 1–2 (3), 1–3 (4), 2–4 (2), 2–5 (6), 3–6 (5)…