Practice/LinkedIn/Leetcode 1473. Paint House III
CodingOptional
You are planning the decoration scheme for a street with several buildings. Each building can be decorated with one of several color schemes, and some buildings may already be decorated (and cannot be changed).
Your goal is to decorate all undecorated buildings such that:
A zone is a maximal contiguous sequence of buildings with the same decoration scheme. For example, if buildings are decorated as [1, 1, 2, 2, 1], there are 3 zones: buildings 0-1 (scheme 1), buildings 2-3 (scheme 2), and building 4 (scheme 1).
Given:
buildings where buildings[i] is either 0 (undecorated) or a positive integer representing its current decoration schemeschemescost where cost[i][j] is the cost to decorate building i with scheme j+1zonesReturn the minimum cost to achieve exactly the target number of zones, or -1 if it's impossible.
Example 1:
` Input: buildings = [0, 0, 0, 0, 0] schemes = 3 cost = [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]] zones = 3
Output: 3
Explanation: One optimal decoration is [1, 1, 2, 2, 3], which creates 3 zones:
Example 2:
` Input: buildings = [3, 1, 2, 3] schemes = 3 cost = [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]] zones = 3
Output: -1
Explanation: All buildings are already decorated as [3, 1, 2, 3], forming 4 zones. Since we cannot repaint pre-decorated buildings, it's impossible to achieve exactly 3 zones. `
Example 3:
` Input: buildings = [1, 2, 2, 1] schemes = 2 cost = [[1, 2], [3, 4], [5, 6], [7, 8]] zones = 3
Output: 0
Explanation: Buildings form zones [1], [2], [1], which is exactly 3 zones. No decoration needed, so cost is 0. `