Data Structures & AlgorithmsHardLeetCode-style · GATE CS

Four houses are painted with one of 3 colours. Painting house i with colour c costs cost[i][c] = [[17, 2, 17], [16, 16, 5], [14, 3, 19], [7, 9, 4]]. Adjacent houses must have different colours. What is the minimum total cost?

  1. A. 14
  2. B. 17
  3. C. 15
  4. D. 13

Correct answer: A. 14

dp[i][c] = cost[i][c] + min over colours k ≠ c of dp[i−1][k]. Painting each house with its cheapest allowed colour in turn is a greedy trap: it can force an expensive colour on a later house.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic