摘要:題目解答利用的思路,只不過把三種顏色換成了種顏色,所以是如何把它的復雜度降到那么就是如果將顏色的部分只掃一遍。參考的里只需要記錄下每個的最小的兩個顏色。
題目:
There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.
The cost of painting each house with a certain color is represented by a n x k cost matrix. For example, costs0 is the cost of painting house 0 with color 0; costs1 is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.
Note:
All costs are positive integers.
Follow up:
Could you solve it in O(nk) runtime?
解答:
利用paint house的思路,只不過把三種顏色換成了k種顏色,所以:
//State: f[i][j] is the minimum cost of painting i houses with color j //Function: f[i][j] = Math.min(f[i - 1][k(except j)]) + costs[i][j]; //Initialize: f[0][j] = costs[0][j]; //Result: Math.min(f[costs.length - 1][j]); public int minCostII(int[][] costs) { if (costs == null || costs.length == 0 || costs[0].length == 0) { return 0; } int house = costs.length, color = costs[0].length; int[][] f = new int[house][color]; for (int i = 0; i < color; i++) { f[0][i] = costs[0][i]; } for (int i = 1; i < house; i++) { for (int j = 0; j < color; j++) { f[i][j] = Integer.MAX_VALUE; for (int k = 0; k < color; k++) { if (k == j) continue; f[i][j] = Math.min(f[i][j], f[i - 1][k] + costs[i][j]); } } } int result = Integer.MAX_VALUE; for (int i = 0; i < color; i++) { result = Math.min(result, f[house - 1][i]); } return result;
followup是如何把它的復雜度降到o(nk), 那么就是如果將顏色的部分只掃一遍。參考leetcode的discuss里most voted answer, 只需要記錄下每個house的最小的兩個顏色。如果下一個顏色跟這個顏色不一樣,就取最小的這個顏色加上這次所選的顏色,并找出最小值;如果下一個顏色跟這個顏色一樣,那么我們不可以取最小的這個顏色,所以我們取第二小的顏色加上這次所選的顏色。最后把最小的顏色輸出就可以了。
//Only the first two minimum costs count, so we keep track on min1 and min2 for each house public int minCostII(int[][] costs) { if (costs == null || costs.length == 0 || costs[0].length == 0) { return 0; } int house = costs.length, color = costs[0].length; int min1 = -1, min2 = -1; for (int i = 0; i < house; i++) { int last1 = min1, last2 = min2; min1 = -1; min2 = -1; for (int j = 0; j < color; j++) { if (j != last1) { costs[i][j] += last1 < 0 ? 0 : costs[i - 1][last1]; } else { costs[i][j] += last2 < 0 ? 0 : costs[i - 1][last2]; } if (min1 < 0 || costs[i][j] < costs[i][min1]) { min2 = min1; min1 = j; } else if (min2 < 0 || costs[i][j] < costs[i][min2]) { min2 = j; } } } return costs[house - 1][min1]; }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/64829.html
摘要:動態規劃復雜度時間空間思路直到房子,其最小的涂色開銷是直到房子的最小涂色開銷,加上房子本身的涂色開銷。我們在原數組上修改,可以做到不用空間。代碼找出最小和次小的,最小的要記錄下標,方便下一輪判斷 Paint House There are a row of n houses, each house can be painted with one of the three colors...
摘要:在原數組上動規,每一行對應一個房子,每一個元素代表從第一行的房子到這一行的房子選擇這一種顏色所花的最小開銷。所以每個元素該元素的值上一行兩個與該元素不同列元素的值的較小者。不過這次要記錄三個變量本行最小值,本行第二小值,本行最小值下標。 Paint House Problem There are a row of n houses, each house can be painted ...
摘要:題目解答這類題還是先找臨時的結果,由臨時的結果最終推出最終解。比如說用存到個的時候最小的但是到第個的時候,有三種情況涂當我涂紅的時候,前面一個只能涂藍或者綠,所以我只能加上這兩種情況的最小值,作為此次計算的最小值,以此類推。 題目:here are a row of n houses, each house can be painted with one of the three co...
摘要:因為取了隊首就不能取隊尾,所以對數組循環兩次,一個從取到,一個從取到,比較兩次循環后隊尾元素,取較大者。注意,要先討論當原數組位數小于時的情況。 Problem After robbing those houses on that street, the thief has found himself a new place for his thievery so that he wi...
摘要:注意對邊界條件的判斷,是否非空,是否長度為 House Robber I Problem You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping y...
閱讀 1409·2021-09-02 09:53
閱讀 2673·2021-07-29 13:50
閱讀 1723·2019-08-30 11:07
閱讀 1577·2019-08-30 11:00
閱讀 1459·2019-08-29 14:00
閱讀 1851·2019-08-29 12:52
閱讀 2569·2019-08-29 11:11
閱讀 3427·2019-08-26 12:23