3429. Paint House IV Medium

@problem@discussion
#Array#Dynamic Programming



1/**
2 * [3429] Paint House IV
3 *
4 * You are given an even integer n representing the number of houses arranged in a straight line, and a 2D array cost of size n x 3, where cost[i][j] represents the cost of painting house i with color j + 1.
5 * The houses will look beautiful if they satisfy the following conditions:
6 * 
7 * 	No two adjacent houses are painted the same color.
8 * 	Houses equidistant from the ends of the row are not painted the same color. For example, if n = 6, houses at positions (0, 5), (1, 4), and (2, 3) are considered equidistant.
9 * 
10 * Return the minimum cost to paint the houses such that they look beautiful.
11 *  
12 * <strong class="example">Example 1:
13 * <div class="example-block">
14 * Input: <span class="example-io">n = 4, cost = [[3,5,7],[6,2,9],[4,8,1],[7,3,5]]</span>
15 * Output: <span class="example-io">9</span>
16 * Explanation:
17 * The optimal painting sequence is [1, 2, 3, 2] with corresponding costs [3, 2, 1, 3]. This satisfies the following conditions:
18 * 
19 * 	No adjacent houses have the same color.
20 * 	Houses at positions 0 and 3 (equidistant from the ends) are not painted the same color (1 != 2).
21 * 	Houses at positions 1 and 2 (equidistant from the ends) are not painted the same color (2 != 3).
22 * 
23 * The minimum cost to paint the houses so that they look beautiful is 3 + 2 + 1 + 3 = 9.
24 * </div>
25 * <strong class="example">Example 2:
26 * <div class="example-block">
27 * Input: <span class="example-io">n = 6, cost = [[2,4,6],[5,3,8],[7,1,9],[4,6,2],[3,5,7],[8,2,4]]</span>
28 * Output: <span class="example-io">18</span>
29 * Explanation:
30 * The optimal painting sequence is [1, 3, 2, 3, 1, 2] with corresponding costs [2, 8, 1, 2, 3, 2]. This satisfies the following conditions:
31 * 
32 * 	No adjacent houses have the same color.
33 * 	Houses at positions 0 and 5 (equidistant from the ends) are not painted the same color (1 != 2).
34 * 	Houses at positions 1 and 4 (equidistant from the ends) are not painted the same color (3 != 1).
35 * 	Houses at positions 2 and 3 (equidistant from the ends) are not painted the same color (2 != 3).
36 * 
37 * The minimum cost to paint the houses so that they look beautiful is 2 + 8 + 1 + 2 + 3 + 2 = 18.
38 * </div>
39 *  
40 * Constraints:
41 * 
42 * 	2 <= n <= 10^5
43 * 	n is even.
44 * 	cost.length == n
45 * 	cost[i].length == 3
46 * 	0 <= cost[i][j] <= 10^5
47 * 
48 */
49pub struct Solution {}
50
51// problem: https://leetcode.com/problems/paint-house-iv/
52// discuss: https://leetcode.com/problems/paint-house-iv/discuss/?currentPage=1&orderBy=most_votes&query=
53
54// submission codes start here
55
56impl Solution {
57    pub fn min_cost(n: i32, cost: Vec<Vec<i32>>) -> i64 {
58        
59    }
60}
61
62// submission codes end
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn test_3429() {
70    }
71}
72


Back
© 2025 bowen.ge All Rights Reserved.