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?
- A. 14
- B. 17
- C. 15
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Events are given as (start day, end day) inclusive: [(1, 2), (2, 3), (3, 4), (1, 2), (3, 5)]. You may attend at most one event per day…
- Split "ababcbacadefegdehijhklij" into the maximum number of contiguous pieces so that each letter appears in at most one piece. What is…
- Unit-time tasks have deadlines [1, 2, 2, 3, 3, 3, 5, 6] (a task must finish by its deadline, and only one task runs at a time from time…
- What is the minimum number of characters you must DELETE from "aabbbcaaddd" so that no two adjacent characters are equal?
- What is the 10-th smallest positive integer that is divisible by NEITHER 3 nor 5?
- What is the length of the longest substring of "abcbxaxba" that becomes a palindrome after deleting AT MOST ONE character from it?