摘要:題目解答利用的思路,只不過(guò)把三種顏色換成了種顏色,所以是如何把它的復(fù)雜度降到那么就是如果將顏色的部分只掃一遍。參考的里只需要記錄下每個(gè)的最小的兩個(gè)顏色。
題目:
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的思路,只不過(guò)把三種顏色換成了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是如何把它的復(fù)雜度降到o(nk), 那么就是如果將顏色的部分只掃一遍。參考leetcode的discuss里most voted answer, 只需要記錄下每個(gè)house的最小的兩個(gè)顏色。如果下一個(gè)顏色跟這個(gè)顏色不一樣,就取最小的這個(gè)顏色加上這次所選的顏色,并找出最小值;如果下一個(gè)顏色跟這個(gè)顏色一樣,那么我們不可以取最小的這個(gè)顏色,所以我們?nèi)〉诙〉念伾由线@次所選的顏色。最后把最小的顏色輸出就可以了。
//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]; }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/64829.html
摘要:動(dòng)態(tài)規(guī)劃復(fù)雜度時(shí)間空間思路直到房子,其最小的涂色開(kāi)銷是直到房子的最小涂色開(kāi)銷,加上房子本身的涂色開(kāi)銷。我們?cè)谠瓟?shù)組上修改,可以做到不用空間。代碼找出最小和次小的,最小的要記錄下標(biāo),方便下一輪判斷 Paint House There are a row of n houses, each house can be painted with one of the three colors...
摘要:在原數(shù)組上動(dòng)規(guī),每一行對(duì)應(yīng)一個(gè)房子,每一個(gè)元素代表從第一行的房子到這一行的房子選擇這一種顏色所花的最小開(kāi)銷。所以每個(gè)元素該元素的值上一行兩個(gè)與該元素不同列元素的值的較小者。不過(guò)這次要記錄三個(gè)變量本行最小值,本行第二小值,本行最小值下標(biāo)。 Paint House Problem There are a row of n houses, each house can be painted ...
摘要:題目解答這類題還是先找臨時(shí)的結(jié)果,由臨時(shí)的結(jié)果最終推出最終解。比如說(shuō)用存到個(gè)的時(shí)候最小的但是到第個(gè)的時(shí)候,有三種情況涂當(dāng)我涂紅的時(shí)候,前面一個(gè)只能涂藍(lán)或者綠,所以我只能加上這兩種情況的最小值,作為此次計(jì)算的最小值,以此類推。 題目:here are a row of n houses, each house can be painted with one of the three co...
摘要:因?yàn)槿×岁?duì)首就不能取隊(duì)尾,所以對(duì)數(shù)組循環(huán)兩次,一個(gè)從取到,一個(gè)從取到,比較兩次循環(huán)后隊(duì)尾元素,取較大者。注意,要先討論當(dāng)原數(shù)組位數(shù)小于時(shí)的情況。 Problem After robbing those houses on that street, the thief has found himself a new place for his thievery so that he wi...
摘要:注意對(duì)邊界條件的判斷,是否非空,是否長(zhǎng)度為 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...
閱讀 1399·2021-09-02 09:53
閱讀 2667·2021-07-29 13:50
閱讀 1715·2019-08-30 11:07
閱讀 1571·2019-08-30 11:00
閱讀 1450·2019-08-29 14:00
閱讀 1844·2019-08-29 12:52
閱讀 2560·2019-08-29 11:11
閱讀 3415·2019-08-26 12:23